gpt4 book ai didi

php - 如何将 select 中的内部查询转换为 Eloquent laravel 5.4

转载 作者:行者123 更新时间:2023-11-29 18:37:20 25 4
gpt4 key购买 nike

我有一个这样的查询..

select *,
case l.user_type
when '0' then
(select CONCAT(first_name,'',last_name) from users where id=l.user_id)
when '1' then
(select party_name from tbl_partys where id=l.user_id)
end as user_name
from tbl_leased_comm l
where l.user_id=$party and l.user_id=$user_id
order by l.updated_at desc

如何将此查询转换为 Laravel 查询任何人都可以帮助我...

最佳答案

是的,它对我有用

作为,

DB::table('tbl_leased_comm')
->select(["*",
DB::raw("case tbl_leased_comm.user_type when '0' then (select CONCAT(first_name,'',last_name) from users where id=tbl_leased_comm.user_id) when '1' then (select party_name from tbl_partys where id=tbl_leased_comm.user_id) end as user_name")])
->where('tbl_leased_comm.user_id','=',$party )
->where('tbl_leased_comm.user_id','=',$user_id)
->orderBy('tbl_leased_comm.updated_at', 'desc')
->get();

非常感谢..

引用链接:Convert mysql query logic to Laravel query builder

关于php - 如何将 select 中的内部查询转换为 Eloquent laravel 5.4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45182624/

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