将mysql查询逻辑转换为Laravel查询生成器

时间:2015-05-02 09:22:36

标签: php mysql laravel laravel-4 query-builder

我正在尝试将我的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');

1 个答案:

答案 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();

以下是它的参考 http://laravel.com/docs/4.2/queries#raw-expressions