【问题标题】:Laravel, why do I have so many queries?Laravel,为什么我有这么多查询?
【发布时间】:2015-06-18 20:29:54
【问题描述】:

控制器

    $attendees = Attendee::with('User')->get();
    return View::make('admin.attendees.index', compact('attendees'));

参会者模型

public function user()                                                                                                                                                                                                                   |        if( !( $user->hasRole('admin') || $user->hasRole('programmer') ))
{                                                                                                                                                                                                                                        |            return Redirect::to('/');
    return $this->belongsTo('User');                                                                                                                                                                                                     |
}    

查看

@foreach($attendees as $attendee)
      <td>{{link_to_route('admin.users.show', $attendee->user->username, $attendee->user->id)}}</td>
@endforeach

223 个查询

select * from `users` where `users`.`id` = '4' limit 1600μs
select `roles`.*, `assigned_roles`.`user_id` as `pivot_user_id`, `assigned_roles`.`role_id` as `pivot_role_id` from `roles` inner join `assigned_roles` on `roles`.`id` = `assigned_roles`.`role_id` where `assigned_roles`.`user_id` = '4'630μs
select * from `attendees`1.24ms
select * from `users` where `users`.`id` in ('5', '1', '3', '8', '9', '10')780μs
select * from `users` where `users`.`id` = '5' limit 1680μs
select * from `users` where `users`.`id` = '5' limit 1650μs
select * from `users` where `users`.`id` = '5' limit 1680μs
select * from `users` where `users`.`id` = '5' limit 1590μs
select * from `users` where `users`.`id` = '1' limit 1
 <continues like so for each user id>

我正在使用phpdebugbar 来显示查询。

迁移

Schema::table('attendees', function(Blueprint $table) {
         $table->foreign('user_id')->references('id')->on('users')
                                   ->onDelete('cascade')
                                   ->onUpdate('no action');

我是否做错了什么导致查询一遍又一遍地运行?

【问题讨论】:

  • 您能在参加者模型中显示您定义的关系吗?
  • 从查询中我可以理解您正在使用Zizaco/entrust 包。我认为这可能是你的问题。在您看来,您返回一个集合。您不查询集合。集合已加载,您可以通过刀片呈现它。如果您使用的是演示者,那就另当别论了。
  • @Chris 已更新以在参加者中显示我的关系。 @akad0 是的,我正在使用 Zizaco/confide/entrust。如果我错了,请纠正我,在我的控制器中,Laravel 对参与者和用户进行查询,以将集合返回到我的视图中,其中包含参与者和相关用户。在我看来,我只想访问该集合,但它似乎正在执行更多查询。
  • 我相信你的急切负载应该是$attendees = Attendee::with('user')-&gt;get();,带有一个小写的U。
  • 你说得对,我在发布更新后才注意到自己。如果你做一个,我会接受你的回答。

标签: laravel laravel-4 eloquent eager-loading


【解决方案1】:

eager load 应该是关系函数的名称,而不是关系的模型,并且显然区分大小写:

$attendees = Attendee::with('user')->get();

【讨论】:

    猜你喜欢
    • 2014-06-28
    • 2011-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多