Php 如何使用Laravel查询生成器跨表选择多个列?
我有一个Laravel雄辩的查询,我试图从MySQL表中选择多个列Php 如何使用Laravel查询生成器跨表选择多个列?,php,laravel,laravel-5,eloquent,laravel-eloquent,Php,Laravel,Laravel 5,Eloquent,Laravel Eloquent,我有一个Laravel雄辩的查询,我试图从MySQL表中选择多个列 $query = DB::connection('global') ->select( 'mytable.id', 'mytable.column1', 'mytable.another_column', 'mytable.created_at', 'myothertable.id
$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函数使用雄辩的查询生成器跨表获取多个列
如何使用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();
您可以阅读是否有不想使用Eloquent ORM的原因?这是一个跨具有联接的表的复杂查询,因此不适合使用Eloquent ORM