gpt4 book ai didi

php - Laravel 使用软删除显示关系数据

转载 作者:行者123 更新时间:2023-12-05 01:44:56 30 4
gpt4 key购买 nike

我想使用 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/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com