Laravel 显示具有软删除的关系数据

Ken*_*eth 3 php laravel laravel-5.4

我想使用 Eloquent:关系在刀片中显示用户的名称。我的代码可以使用关系显示数据,但如果数据被软删除,则会出现错误。

这是我的代码。

// 历史模型//

public function user()
{
    return $this->belongsTo(User::class);
}
Run Code Online (Sandbox Code Playgroud)

// 用户模型 //

public function history()
{
    return $this->hasMany(History::class);
}
Run Code Online (Sandbox Code Playgroud)

// 控制器 //

public function index()
{
    $histories = History::find(3);
    return view('booking.backend.content.history.index', compact('histories'));
}
Run Code Online (Sandbox Code Playgroud)

//索引.刀片//

{{$history->user->name}}
Run Code Online (Sandbox Code Playgroud)

Ken*_*eth 6

我已经修好了。在我的模型历史中我添加了withtrashed。

public function history()
{
    return $this->hasMany(History::class)->withTrashed();
}
Run Code Online (Sandbox Code Playgroud)