gpt4 book ai didi

php - 当有 DB::select() 时如何使用 Laravel 关系?

转载 作者:行者123 更新时间:2023-11-29 17:41:48 25 4
gpt4 key购买 nike

这是我的代码:

$guarantee_tickets =  DB::select("(select ga.id, '' as unique_product_id, ga.user_id, ga.submit_time, '' as title, ga.description, '' as  tracking_code, '' as closed, '' as close_date, ga.isAdminNote from guarantee_tickets_answers ga where guarantee_tickets_id = $request->id order by ga.id desc)
union all
(select gt.id, gt.unique_product_id, gt.user_id, gt.submit_time, gt.title, gt.description, gt.tracking_code, gt.closed, gt.close_date, '' from guarantee_tickets gt where id = $request->id)");

还有这个:

dd($guarantee_tickets->user->name);

抛出:

Undefined property: stdClass::$user

如何修复它?

<小时/>

注意到我在 guarantee_ticketsguarantee_tickets_answers 模型中都有这个:

public function user()
{
return $this->hasOne(User::class, 'id', 'user_id');
}

最佳答案

当您使用DB::Select执行查询时,您正在对数据库执行原始SQL查询。您将收到一个 stdClass 实例,其中包含数据库中的属性。您可以阅读更多相关信息 here .

如果您希望能够访问这些关系,那么您需要执行 Eloquent 查询来加载数据。

关于php - 当有 DB::select() 时如何使用 Laravel 关系?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49964005/

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