1
で複数のテーブルと行動を通じて5.雄弁関係:私は次のモデルを持っているLaravelのforeach
Shop_list:
public function shopListItem()
{
return $this->hasMany(Shopping_list_item::class, 'shopping_list_id');
}
Shopping_list_item:
public function shoppingList()
{
return $this->belongsTo(Product::class);
}
public function product()
{
return $this->belongsTo(Product::class);
}
製品:
public function shopListItem()
{
return $this->hasMany(Shopping_list_item::class);
}
私はこのコードを実行すると:
{"id":1,"shopping_list_id":13,"product_id":69,"quantity":4,"created_at":"2016-09-05 19:23:35","updated_at":"2016-09-05 19:34:53"}
しかし、私がループしたいとidを取得する場合:
@foreach($shoppinglists as $sh)
{{$sh->shopListItem()->id}}
@endforeach
{{$shoppinglists->shopListItem()->first()}}
を私は次のように正しい結果を得ます次に、次のエラーが表示されます。
Call to a member function shopListItem() on boolean
質問:なぜループでオブジェクトがブール値に変換されるのですか?ループする正しい方法は何ですか?
今、私は次のエラーを取得する: **は非のプロパティを取得しようとすると、オブジェクト** –
これは 'hasMany'関係ですので、それを更新してください。 – aynber
ありがとう! 'shopListItem'と' shopListItem() 'の違いについて私が読むことができるリンクを教えてください。もっと分かりましたか? –