Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.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-多个查询函数,用于多个已加载的关系_Php_Laravel - Fatal编程技术网

Php Laravel-多个查询函数,用于多个已加载的关系

Php Laravel-多个查询函数,用于多个已加载的关系,php,laravel,Php,Laravel,我想知道是否有一种方法可以解决加载两个hasOne关系的多闭包问题。对于一个关系(personaldata),我只需要一些列,第二个关系(userfile)有一个where子句。 如果我从personaldata中删除select,或者从userfile中删除where子句,那么它就可以工作,否则有两个闭包会使其中一个闭包的值为null $user = Users::with(['personaldata' => function($a) { $a

我想知道是否有一种方法可以解决加载两个hasOne关系的多闭包问题。对于一个关系(personaldata),我只需要一些列,第二个关系(userfile)有一个where子句。 如果我从personaldata中删除select,或者从userfile中删除where子句,那么它就可以工作,否则有两个闭包会使其中一个闭包的值为null

 $user = Users::with(['personaldata' => function($a) {
                    $a->select('first_name', 'title_name', 'last_name', 'street_address', 'city', 'state', 'country_code', 'zip_code', 'telephone_data', 'skype', 'per_street_address', 'per_city', 'per_state', 'per_country_code', 'per_zip_code');
                },
                'userfile' => function($b) {
                    $b->where('type', '1');
                }
                ])
                ->where('user_id', Auth::id())
                ->get();

按如下方式更改代码:

$user = Users::with(['personaldata' => function($query) {
            $query->select('first_name', 'title_name', 'last_name', 'street_address', 'city', 'state', 'country_code', 'zip_code', 'telephone_data', 'skype', 'per_street_address', 'per_city', 'per_state', 'per_country_code', 'per_zip_code');
        }])
        ->with(['userfile' => function($query) {
            $query->where('type', '1');
        }])
        ->where('user_id', Auth::id())
        ->get();

为什么?好的答案总是会有一个解释,说明做了什么以及为什么这样做,不仅是为了OP,而且是为了未来的访问者,这样他们可能会发现这个问题并阅读你的答案。非常感谢,伙计。我来这里只是想看看是否允许使用多个->with()方法,谢谢。