Laravel 访问关系时忽略预先加载的关系 属性

Laravel ignoring eager loaded relationship when accessing relation property

我有两个实体,NewCar 和 CapModel。我使用的关系是 belongsTo,其中每个 NewCar 都属于一个 CapModel。

对应的join是:NewCar.model_code = CapModel.mod_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');
}

我的 CapModel 模型:

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 查询?代码 "works" 因为它得到 属性,但它 运行 有 1700 个查询,每辆车 1 个。

NewCar::with('capmodel')改成NewCar::with('capModel'),我遇到过类似的情况,希望对你有帮助