我有这种关系:
商店可能有很多产品,每种产品都有很多效果
stores
和products
- >多对多关系
products
和effects
- >一对多(一种产品可以有多种效果)
以下是我的关系:
商店模式
public function products()
{
return $this->belongsToMany('App\Models\Product');
}
产品型号
public function stores()
{
return $this->belongsToMany('App\Models\Store');
}
public function effects()
{
return $this->hasMany('App\Models\Effect');
}
效果模型
public function products()
{
return $this->belongsTo('App\Models\Product');
}
现在,我可以访问与产品的关系,但是当我访问效果关系时:
$store = Store::where('slug', 'cantina')->first();
dd($store->products->first()->effects);
dd返回一个字符串,其中包含关系名称:
"effects"
之前我从未见过这个“错误”,任何人都知道为什么关系会返回一个字符串而不是那个雄辩的?
[编辑]
如果我在关系调用中对任何内容进行数字处理,它们会输出我所挖掘的内容:
dd($store->products->first()->effects);
//Output:
"effects"
另一个例子:
dd($store->products->first()->foobar);
//Output:
"foobar"