如何在laravel 5.4中创建一个假的列名?

时间:2017-08-24 02:57:52

标签: php laravel

我有一个需要在Laravel中使用假列名的查询。代码很好,但问题是1000000000138719360 '"0" as lang_id',

我需要这样做的原因,我之前的表使用旧的列名。所以我需要提供假的列名,以便与我之前的代码匹配。

任何人都可以帮助我吗?感谢。

当前代码:

'"0" as qst_story',

问题:

$question_content = DB::connection('mysql_pencil_content')
                ->table('cms_qst_master_content as qmc')
//                ->with(['ImageContentGallery'])
                ->leftJoin('cms_qst_explaination as exp','exp.explaination_id','=','qmc.content_explaination_id')
                ->where('qmc.question_id',$main['question_id'])
//                ->groupBy('lang_id')
                ->get(['qmc.*',
                    'qmc.content_id as qst_cnt_id',
                    'qmc.question_id',
                    'qmc.content_text as qst_cnt_text',
                    'qmc.content_option as qst_cnt_options',
                    '"0" as qst_story',
                    'exp.explaination_text as explanation',
                    '"0" as lang_id',
                    'qmc.content_image_id as image_id',
                    'qmc.dt_update'])->toSql();
        pre($question_content);

2 个答案:

答案 0 :(得分:1)

您可以使用DB::raw

DB::connection('mysql_pencil_content')
                ->table('cms_qst_master_content as qmc')
//                ->with(['ImageContentGallery'])
                ->leftJoin('cms_qst_explaination as exp','exp.explaination_id','=','qmc.content_explaination_id')
                ->where('qmc.question_id',$main['question_id'])
->select('qmc.*',
        'qmc.content_id as qst_cnt_id',
        'qmc.question_id',
        'qmc.content_text as qst_cnt_text',
        'qmc.content_option as qst_cnt_options',
         DB::raw('0 as qst_story'),
         'exp.explaination_text as explanation',
          DB::raw('0 as lang_id'),
         'qmc.content_image_id as image_id',
        'qmc.dt_update'
 )
->get(

答案 1 :(得分:0)

我最好的方法是使用口才 因此在模型中发挥作用

class YourModal {
.
.
   public function getFakeitAttribute(){
        return "Fake Column";
   }
.
.
}

现在使用它

$whatever = YourModal::all()->first();

// set value to it
$whatever->fakeit = 'other Fake';

// get the data of fake field
echo $whatever->fakeit