我想使用 Eloquent: Relationships 在 Blade 中显示用户名。我的代码可以使用关系显示数据,但如果数据被软删除,它会给我一个错误。
这是我的代码。
//历史模型//
public function user()
{
return $this->belongsTo(User::class);
}
//用户模型//
public function history()
{
return $this->hasMany(History::class);
}
// Controller //
public function index()
{
$histories = History::find(3);
return view('booking.backend.content.history.index', compact('histories'));
}
//索引.blade//
{{$history->user->name}}
最佳答案
我已经修好了。 在我的模型历史记录中,我添加了 withtrashed。
public function history()
{
return $this->hasMany(History::class)->withTrashed();
}
关于php - Laravel 使用软删除显示关系数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44039423/