找到你要的答案

Q:Convert mysql query logic to Laravel query builder

Q:将MySQL查询逻辑laravel查询生成器

I am trying to convert my mysql query logic to Laravel query builder. I I have no Idea how to convert it as laravel query.

my query logic is

SELECT id,name,
case 
    when visibility_status = '1' 
    then 'Visible' 
    when visibility_status = '0' 
    then 'Invisible'
    end as visibility_status FROM `flowers`

generally I write a select query using query builder but cant implement above logic

$result = DB::table('flowers')
        ->select('flowers.id as id', 'flowers.name as name',
'flowers.visibility_status as visibility_status');

我试图把我的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`

一般我使用查询生成器编写一个选择查询但不能实现上述逻辑

$result = DB::table('flowers')
        ->select('flowers.id as id', 'flowers.name as name',
'flowers.visibility_status as visibility_status');
answer1: 回答1:

Try This

$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();

Here is the reference for it http://laravel.com/docs/4.2/queries#raw-expressions

试试这个

$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();

Here is the reference for it http://laravel.com/docs/4.2/queries#raw-expressions

php  mysql  laravel  laravel-4  query-builder