Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在Laravel 5.5中插入自联接的分块值_Php_Laravel_Eloquent_Laravel Query Builder - Fatal编程技术网

Php 在Laravel 5.5中插入自联接的分块值

Php 在Laravel 5.5中插入自联接的分块值,php,laravel,eloquent,laravel-query-builder,Php,Laravel,Eloquent,Laravel Query Builder,因此,我有一个查询,它包含一个自连接,并且是分块的。我试图运行查询,但得到: 调用未定义的方法stdClass::pull 如何在foreach循环中选择u1.id、u2.id、u1.name、u2.name?结果是1500.000行大,因此不进行分块是不好的。我使用Postgres作为数据库。使用select方法影响所选行u1.id、u2.id、u1.name、u2.name的别名,然后使用该别名获取数据: DB::table('locations as u1') ->join(

因此,我有一个查询,它包含一个自连接,并且是分块的。我试图运行查询,但得到:

调用未定义的方法stdClass::pull


如何在foreach循环中选择u1.id、u2.id、u1.name、u2.name?结果是1500.000行大,因此不进行分块是不好的。我使用Postgres作为数据库。

使用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();
        }
    });

注意:Pull方法仅适用于集合,在foreach中,您从文档中获取的每一行都是对象stdClass,Pull仅用于集合。我想$route不是一个集合。@Vincent Decaux我理解它,但是如何将这些值放入foreach循环中呢?这只是一个想法。选择位置作为组件有什么诀窍吗?$route包含什么?@Vincent Decaux它包含分块位置集合。我们需要的英雄,但不配。非常感谢你:D!
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();
        }
    });