【问题标题】:Laravel 7 Eloquent relationships not workingLaravel 7雄辩的关系不起作用
【发布时间】:2020-08-27 22:57:13
【问题描述】:

我正在使用 laravel 7。我有 2 个表格、产品和推荐。每个推荐都与产品相关。所以我建立了两个关系:

  • 产品:hasMany('App\Models\OM\Testimonial');
  • 客户评价:belongsTo('App\Models\OM\Product', 'product_id')

但是当我 dd(Testimonial->with('product)) 我得到这个

array:1 [▼ “推荐” => Illuminate\Database\Eloquent\Builder {#347 ▼ #query: Illuminate\Database\Query\Builder {#358 ▶} #model: App\Models\OM\Testimonial {#359 ▼ #table:“om_testimonials” #fillable: 数组:4 [▶] #连接:空 #primaryKey:“ID” #keyType:“整数” +递增:真 #和: [] #withCount: [] #perPage: 15 +存在:假 +wasRecentlyCreated: 假 #属性: [] #原版的: [] #变化: [] #casts:[] #classCastCache:[] #日期: [] #dateFormat:空 #追加:[] #dispatchesEvents:[] #observables:[] #关系:[] #触摸:[] +时间戳:真 #隐藏:[] #可见的: [] #guarded:数组:1 [▶] } #eagerLoad:数组:1 [▶] #localMacros:[] #onDelete:空 #passthru:数组:19 [▶] #范围:[] #removedScopes: [] } ]

【问题讨论】:

  • Testimonial::with('product')->get(); 这会起作用

标签: php laravel eloquent eloquent-relationship


【解决方案1】:

这很正常,with 要求 Eloquent 急切加载您的关系,但它尚未检索它,因为它允许您在“查询”上添加约束。您需要这样做来检索您的模型及其关系

Testimonial::with('product')->get();

您还应该查看文档,每个细节都在这里:https://laravel.com/docs/7.x/eloquent-relationships#eager-loading

【讨论】:

  • 好吧,我太愚蠢了:)))。我错过了 de->get()。谢谢!
猜你喜欢
  • 2023-04-07
  • 2019-08-13
  • 1970-01-01
  • 1970-01-01
  • 2023-04-01
  • 1970-01-01
  • 2020-05-30
  • 2019-06-07
相关资源
最近更新 更多