我有一个Laravel Eloquent查询,我试图从MySQL表中选择多个列。
$query = DB::connection('global')
->select(
'mytable.id',
'mytable.column1',
'mytable.another_column',
'mytable.created_at',
'myothertable.id
)
->from('mytable')
->get();
看起来select()函数有三个参数:query,bindings和useReadPdo。上面的查询给出了一个错误:
{"error":true,"message":"Type error: Argument 1 passed to Illuminate\\Database\\Connection::prepareBindings() must be of the type array, string given" }
如何使用Laravel查询构建器为上述列编写select?
我正在以这种方式构造查询,因为我希望在另一个表中进行连接,如下所示:
$query = DB::connection('global')
->select(
'mytable.id',
'mytable.column1',
'mytable.another_column',
'mytable.created_at',
'myothertable.id
)
->from('mytable')
->leftJoin('myothertable', function($join){
$join->on('mytable.id', '=', 'myothertable.id');
})
->get();
如何使用select函数通过Eloquent查询构建器在表中获取多个列?
答案 0 :(得分:2)
如何使用Laravel查询构建器为上述列编写select?
你可以这样做:
$data = DB::table('mytable')
->join('myothertable', 'mytable.id', '=', 'myothertable.mytable_id')
->select(
'mytable.id',
'mytable.column1',
'mytable.another_column',
'mytable.created_at',
'myothertable.id'
)
->get();
您可以阅读documentations here