Laravel在访问关系属性时忽略了急切的加载关系

时间:2015-02-11 12:44:23

标签: php laravel eloquent blade

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

相应的联接是: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'));
}

我的NewCar模型:

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个查询?代码"工作"因为它获得了属性,但是它运行了1700个查询,每个车只有1个。

1 个答案:

答案 0 :(得分:1)

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