Con*_*ech 5 php laravel eloquent laravel-5
我有一个 Laravel Eloquent 查询,我试图从 MySQL 表中选择多个列。
$query = DB::connection('global')
->select(
'mytable.id',
'mytable.column1',
'mytable.another_column',
'mytable.created_at',
'myothertable.id
)
->from('mytable')
->get();
Run Code Online (Sandbox Code Playgroud)
看起来 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" }
Run Code Online (Sandbox Code Playgroud)
如何使用 Laravel 查询生成器为上述列编写选择?
我以这种方式构造查询,因为我希望在另一个表之间进行联接,如下所示:
$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();
Run Code Online (Sandbox Code Playgroud)
如何使用 Eloquent 查询生成器使用 select 函数跨表抓取多个列?
如何使用 Laravel 查询生成器为上述列编写选择?
你可以做:
$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();
Run Code Online (Sandbox Code Playgroud)
您可以在此处阅读文档