我正在尝试将我的mysql查询逻辑转换为Laravel查询构建器。我不知道如何将其转换为laravel查询。
我的查询逻辑是
SELECT id,name,
case
when visibility_status = '1'
then 'Visible'
when visibility_status = '0'
then 'Invisible'
end as visibility_status FROM `flowers`
通常我使用查询构建器编写一个select查询,但不能在逻辑
之上实现$result = DB::table('flowers')
->select('flowers.id as id', 'flowers.name as name',
'flowers.visibility_status as visibility_status');
答案 0 :(得分:7)
试试这个
$users = DB::table('flowers')
->select(["id", "name",
DB::raw("
case
when visibility_status = '1'
then 'Visible'
when visibility_status = '0'
then 'Invisible'
end as visibility_status
")])->get();