Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/257.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_Eloquent_Blade - Fatal编程技术网

Php Laravel在访问关系属性时忽略已加载的关系

Php Laravel在访问关系属性时忽略已加载的关系,php,laravel,eloquent,blade,Php,Laravel,Eloquent,Blade,我有两个实体,NewCar和CapModel。我使用的关系是belongsTo,即每辆新车都属于一个CapModel 相应的联接是:NewCar.model\u code=CapModel.mod\u code 我的控制器代码使用即时加载: public function rawData() { $cars = NewCar::with('capmodel')->take(20)->get(); return View::make('new-cars.raw-dat

我有两个实体,NewCar和CapModel。我使用的关系是belongsTo,即每辆新车都属于一个CapModel

相应的联接是:
NewCar.model\u code=CapModel.mod\u code

我的控制器代码使用即时加载:

public function rawData()
{
    $cars = NewCar::with('capmodel')->take(20)->get();

    return View::make('new-cars.raw-data', compact('cars'));
}
我的新车型号:

public function capModel()
{
    return $this->belongsTo('CapModel', 'model_code', 'mod_code');
}
我的模型:

public function cars()
{
    return $this->hasMany('NewCar', 'model_code', 'mod_code');
}
我的视图,正在尝试访问已加载模型的属性:

{{ $car->capModel->body_styles }}
最后是运行的查询。注意,即时加载发生在顶部,但随后也会运行后续查询

select * from `listings_new_cars`265ms
select * from `cap_model` where `cap_model`.`mod_code` in ('61163', '61162', '53880', '53869', '53848', '53847', '50253', '50250', '64738', '52477', '52476', '52475', '52474', '64736', '64735', '59178', '59177', '66648', '66639', '61321', '61287', '44962', '52707', '52703', '52716', '52712', '50006', '50000', '50687', '52736', '57436', '47162', '50499', '49830', '44985', '51566', '51565', '57165', '51941', '66160', '66154', '66144', '49569', '49554', '49615', '49681', '49670', '49742', '48973', '58273', '58268', '46090', '50536', '46091', '51585', '60772', '62079', '62078', '62059', '62058', '52797', '54484', '46663', '46662', '66719', '66708', '61433', '53136', '53129', '64355', '64352', '61129', '59771', '59706', '54969', '62587', '63176', '63172', '66137', '57096', '57095', '56799', '56798', '49822', '49821', '58451', '50979', '50978', '66424', '50977', '50976', '65022', '64213', '64206', '63262', '63253', '50981', '50980', '64926', '64912', '64871', '64857', '48072', '48052', '59090', '67120', '67112', '59200', '61878', '61877', '67071', '67085', '65972', '67853', '67778', '67337', '67336', '67358', '66759', '66758', '67896', '67939', '67940', '67899', '67105', '67098', '68435', '52578', '68609', '68603', '68937', '68897', '68896', '68866', '69418', '69420', '69416', '69847', '69838', '69810', '69757', '69692', '69691', '70133', '70213', '70195', '70210', '70171', '70177', '70294', '69672', '69642', '69661', '69631', '69837', '71435', '71422', '71413', '71404', '71376', '71364', '71718', '71609', '71598', '72209', '71867', '71872', '71842', '71841', '71843', '71875', '72776', '72781')26ms
select * from `cap_model` where `cap_model`.`mod_code` = '61163' limit 1 17ms
select * from `cap_model` where `cap_model`.`mod_code` = '61163' limit 1 18ms
select * from `cap_model` where `cap_model`.`mod_code` = '61163' limit 1
select * from `cap_model` where `cap_model`.`mod_code` = '61163' limit 1 18ms
select * from `cap_model` where `cap_model`.`mod_code` = '61163' limit 1 19ms
select * from `cap_model` where `cap_model`.`mod_code` = '61163' limit 1 17ms
select * from `cap_model` where `cap_model`.`mod_code` = '61163' limit 1 17ms
select * from `cap_model` where `cap_model`.`mod_code` = '61163' limit 1 17ms
select * from `cap_model` where `cap_model`.`mod_code` = '61163' limit 1 17ms
select * from `cap_model` where `cap_model`.`mod_code` = '61163' limit 1 19ms
select * from `cap_model` where `cap_model`.`mod_code` = '61163' limit 1 19ms
select * from `cap_model` where `cap_model`.`mod_code` = '61163' limit 1 17ms
select * from `cap_model` where `cap_model`.`mod_code` = '61163' limit 1 18ms
select * from `cap_model` where `cap_model`.`mod_code` = '61163' limit 1
那么,为什么Laravel忽略了我急切的加载,而是运行n+1查询呢?代码“起作用”是因为它获取属性,但它运行1700个查询,每辆车1个查询。

NewCar::with('capmodel')
更改为
NewCar::with('capmodel')
,我遇到过类似的情况,可能会对您有所帮助