所以,我有一个包含自连接的查询并且被分块。我试图运行查询,但得到:
调用未定义的方法stdClass :: pluck()
DB::table('locations as u1')
->join('locations as u2','u1.id', '<>', 'u2.id')
->whereNotExists(function($query)
{
$query->select(DB::raw(1))
->from('routes')
->whereRaw('routes.from_id = u1.id')
->whereRaw('routes.to_id = u2.id');
})->orderBy('u1.id')->chunk(5000, function ($routes) {
foreach ($routes as $route){
$db = new Route;
$db->from_id = $route->pluck('u1.id');
$db->to_id = $route->pluck('u2.id');
$db->distance = Flow::GIS('OSRM')->route($route->pluck('u1.name'),$route->pluck('u2.name'))->distance();
$db->save();
}
});
如何在foreach循环中选择 u1.id,u2.id,u1.name,u2.name ?结果是1.500.000行大,所以这样做没有分块是我正在使用Postgres作为我的数据库。
答案 0 :(得分:0)
使用Select方法影响所选行的别名(u1.id,u2.id,u1.name,u2.name),然后使用该别名获取数据:
DB::table('locations as u1')
->join('locations as u2','u1.id', '<>', 'u2.id')
->whereNotExists(function($query) {
$query->select(DB::raw(1))
->from('routes')
->whereRaw('routes.from_id = u1.id')
->whereRaw('routes.to_id = u2.id');
})
->select( \DB::raw("u1.id as u1id, u2.id as u2.id, u1.name as u1name, u2.name as u2name") )
->orderBy('u1.id')
->chunk(5000, function ($routes) {
foreach ($routes as $route){
$db = new Route;
$db->from_id = $route->u1id;
$db->to_id = $route->u2id;
$db->distance = Flow::GIS('OSRM')->route($route->u1name, $route->u2name)->distance();
$db->save();
}
});
注意:pluck方法仅适用于集合,在foreach中您将每行作为对象(stdClass)