【发布时间】:2021-05-19 06:02:46
【问题描述】:
我有两个表订单和状态。它与数据透视表 order_status 相关
在我的Order 模型中
public function status()
{
return $this->belongsToMany('App\Status')->withTimestamps();
}
在我的Status 模型中
public function order()
{
return $this->belongsToMany('App\Order')->withTimestamps();
}
在控制器中
$items = Order::orderBy('id','desc')->with('status')->paginate(20);
return view('dashboard.index', compact('items'));
现在在刀片中。
@foreach($items as $key=>$row)
{{-- {{ dd($row->status()->orderBy('pivot_created_at','DESC')->first()->message ) }} --}} //it works if checked by dd()
<tr>
<td>{{ $row->status()->orderBy('pivot_created_at','DESC')->first()->message }}</span></td> //here it not works and says Trying to get property 'message' of non-object
</tr>
@endforeach
请检查我的代码。我再次重复我的问题。当我通过 dd() 检查输出时,它会清楚地返回,但是当我想在刀片中显示时,它会返回我尝试获取非对象的属性“消息”。我现在能做什么?我在另一个对我有用的项目中做到了,但现在它返回错误。
我的 php 版本:7.4
【问题讨论】:
标签: php laravel laravel-blade