[英]Error with Eloquent one to many relationship
一對多模型存在問題。 我知道要訪問1到許多,需要發送一個foreach循環。 我這樣做並獲得空白。
項目型號
class Item extends Model
{
protected $table = 'items';
public function offers(){
return $this->hasMany('App\Offer','listing_id');
}
優惠模式
class Offer extends Model
{
protected $table = 'offers';
public function item(){
return $this->belongsTo('App\Item','listing_id');
}
}
物料控制器:
public function index()
{
$user_id = auth()->user()->id;
$listings = Item::with('offers')->where('user_id','1')->paginate(2);;
return view('user.dashboard')->with('listings',$listings);
}
視圖:
@foreach ($listings as $listing)
{{listing->offer_price}}
@endforeach
因此,我試圖通過{{listing-> offer_price}}從offer表中引用一個值,但視圖中什么都沒顯示-空白? 每個項目都有多個優惠
您需要兩個循環:
@foreach ($listings as $listing)
@foreach ($listing->offers as $offer)
{{ $offer->price }}
@endforeach
@endforeach
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.