Laravel数组在数据数组之前显示为null
我有一个具有许多属性的用户。这是用户也应该能够查看提供的赌注对他的财产 因此,建立起这种关系 User.phpLaravel数组在数据数组之前显示为null,laravel,Laravel,我有一个具有许多属性的用户。这是用户也应该能够查看提供的赌注对他的财产 因此,建立起这种关系 User.php public function properties(){ return $this->hasMany('App\Property'); } Property.php public function offers(){ return $this->hasMany('App\Offer'); } 然后在我的控制器中,这就是我拥有的: public func
public function properties(){
return $this->hasMany('App\Property');
}
Property.php
public function offers(){
return $this->hasMany('App\Offer');
}
然后在我的控制器中,这就是我拥有的:
public function my_offers(){
$properties = Property::whereUserId(Auth::id())->get();
return view('pages.seller.offers.index', compact('properties'));
}
然后我会这样谈我的观点:
@if($properties)
<ul>
@foreach($properties as $property)
<li>{{$property->offers->offer_message}}</li>
@endforeach
</ul>
@endif
当我查看页面时,我看到以下错误:
此集合实例上不存在属性[offer_message]
但是这个属性存在于我的表中
如果我将列表项更改为下面的列表项,我可以看到数组:
<li>{{$property->offers}}</li>
我还看到,在包含数据的数组之前和之后,有两个空数组,如下图所示:
有什么我没有说对的吗?如果不是所有的属性都有优惠,那么你应该检查,在优惠是一个集合之前,你需要遍历它,这就是为什么你会出现错误
@if($properties)
@php ($i = 1)
<ul>
@foreach($properties as $property)
@if ($property->offers)
@foreach ($property->offers as $offer)
<li>Offer {{ $i++ }}: {{$offer->offer_message}}</li>
@endforeach
@endif
@endforeach
</ul>
@endif
你可能应该了解这种关系:
$properties = Property::whereUserId(Auth::id())->has('offers')->with('offers')->get();
嗯,似乎不是所有的酒店都有优惠。这是你想要的吗?不,不是所有的酒店都有。请看我的编辑。报价是一个集合,您需要在其中循环。Giiz。我确实注意到了这一点。它现在起作用了,除此之外,我真的很感谢你们的支持。我怎么给它们编号。假设我有3个提议,那个么系统应该说提议1,提议2,所以我可以提供帮助。如果保留has'offers',则可以删除If$property->offers。您需要在每个属性中对它们进行编号吗?我的意思是,当你循环一个新属性时,计数是否应该再次从1开始?这可以帮助你:是的,我需要相应地给它们编号
$properties = Property::whereUserId(Auth::id())->has('offers')->with('offers')->get();