提问者:小点点

Laravel如何减少查询计数,渴望加载?


假设我有3个模型

  • 用户
  • 与会者
  • 调度程序段

与会者属于用户

  • 用户有许多与会者

预订是一种轴心关系

  • 与会者属于日程安排的日程安排部分(透视表=预订

我得到了pivot关系的计数,我有大约600多个查询,因为它一直在为每个计划的\u程序段\u id查询计数。。。我不知道如何在一个查询中获得所有这些信息。。。急切的负载或其他。

正在运行的查询(由debugbar显示)

select * from `scheduled_program_segments` where `scheduled_program_segments`.`scheduled_program_id` = '477'410μs
select count(*) as aggregate from `attendees` inner join `bookings` on `attendees`.`id` = `bookings`.`attendee_id` where `bookings`.`scheduled_program_segment_id` = '1392' and `bookings`.`registered` = '1'450μs
select count(*) as aggregate from `attendees` inner join `bookings` on `attendees`.`id` = `bookings`.`attendee_id` where `bookings`.`scheduled_program_segment_id` = '1392' and `bookings`.`registered` = '1'390μs
select * from `scheduled_program_segments` where `scheduled_program_segments`.`scheduled_program_id` = '478'420μs
select count(*) as aggregate from `attendees` inner join `bookings` on `attendees`.`id` = `bookings`.`attendee_id` where `bookings`.`scheduled_program_segment_id` = '1393' and `bookings`.`registered` = '1'390μs
select count(*) as aggregate from `attendees` inner join `bookings` on `attendees`.`id` = `bookings`.`attendee_id` where `bookings`.`scheduled_program_segment_id` = '1393' and `bookings`.`registered` = '1'390μs
select * from `program_sessions` where `program_sessions`.`id` = '23' limit 1390μs
select * from `scheduled_program_segments` where `scheduled_program_segments`.`scheduled_program_id` = '478'450μs
select count(*) as aggregate from `attendees` inner join `bookings` on `attendees`.`id` = `bookings`.`attendee_id` where `bookings`.`scheduled_program_segment_id` = '1393' and `bookings`.`registered` = '1'390μs
select count(*) as aggregate from `attendees` inner join `bookings` on `attendees`.`id` = `bookings`.`attendee_id` where `bookings`.`scheduled_program_segment_id` = '1393' and `bookings`.`registered` = '1'380μs
select * from `scheduled_program_segments` where `scheduled_program_segments`.`scheduled_program_id` = '481'730μs
select count(*) as aggregate from `attendees` inner join `bookings` on `attendees`.`id` = `bookings`.`attendee_id` where `bookings`.`scheduled_program_segment_id` = '1396' and `bookings`.`registered` = '1'400μs
select count(*) as aggregate from `attendees` inner join `bookings` on `attendees`.`id` = `bookings`.`attendee_id` where `bookings`.`scheduled_program_segment_id` = '1396' and `bookings`.`registered` = '1'390μs
select count(*) as aggregate from `attendees` inner join `bookings` on `attendees`.`id` = `bookings`.`attendee_id` where `bookings`.`scheduled_program_segment_id` = '1397' and `bookings`.`registered` = '1'390μs
select count(*) as aggregate from `attendees` inner join `bookings` on `attendees`.`id` = `bookings`.`attendee_id` where `bookings`.`scheduled_program_segment_id` = '1398' and `bookings`.`registered` = '1'390μs
select count(*) as aggregate from `attendees` inner join `bookings` on `attendees`.`id` = `bookings`.`attendee_id` where `bookings`.`scheduled_program_segment_id` = '1399' and `bookings`.`registered` = '1'390μs
select count(*) as aggregate from `attendees` inner join `bookings` on `attendees`.`id` = `bookings`.`attendee_id` where `bookings`.`scheduled_program_segment_id` = '1400' and `bookings`.`registered` = '1'380μs
select count(*) as aggregate from `attendees` inner join `bookings` on `attendees`.`id` = `bookings`.`attendee_id` where `bookings`.`scheduled_program_segment_id` = '1401' and `bookings`.`registered` = '1'380μs
select count(*) as aggregate from `attendees` inner join `bookings` on `attendees`.`id` = `bookings`.`attendee_id` where `bookings`.`scheduled_program_segment_id` = '1402' and `bookings`.`registered` = '1'440μs
select count(*) as aggregate from `attendees` inner join `bookings` on `attendees`.`id` = `bookings`.`attendee_id` where `bookings`.`scheduled_program_segment_id` = '1403' and `bookings`.`registered` = '1'380μs
select count(*) as aggregate from `attendees` inner join `bookings` on `attendees`.`id` = `bookings`.`attendee_id` where `bookings`.`scheduled_program_segment_id` = '1404' and `bookings`.`registered` = '1'380μs
select count(*) as aggregate from `attendees` inner join `bookings` on `attendees`.`id` = `bookings`.`attendee_id` where `bookings`.`scheduled_program_segment_id` = '1405' and `bookings`.`registered` = '1'380μs
select count(*) as aggregate from `attendees` inner join `bookings` on `attendees`.`id` = `bookings`.`attendee_id` where `bookings`.`scheduled_program_segment_id` = '1406' and `bookings`.`registered` = '1'380μs
select count(*) as aggregate from `attendees` inner join `bookings` on `attendees`.`id` = `bookings`.`attendee_id` where `bookings`.`scheduled_program_segment_id` = '1407' and `bookings`.`registered` = '1'380μs
select count(*) as aggregate from `attendees` inner join `bookings` on `attendees`.`id` = `bookings`.`attendee_id` where `bookings`.`scheduled_program_segment_id` = '1408' and `bookings`.`registered` = '1'380μs
select count(*) as aggregate from `attendees` inner join `bookings` on `attendees`.`id` = `bookings`.`attendee_id` where `bookings`.`scheduled_program_segment_id` = '1409' and `bookings`.`registered` = '1'380μs
select count(*) as aggregate from `attendees` inner join `bookings` on `attendees`.`id` = `bookings`.`attendee_id` where `bookings`.`scheduled_program_segment_id` = '1410' and `bookings`.`registered` = '1'380μs
select count(*) as aggregate from `attendees` inner join `bookings` on `attendees`.`id` = `bookings`.`attendee_id` where `bookings`.`scheduled_program_segment_id` = '1411' and `bookings`.`registered` = '1'380μs
select count(*) as aggregate from `attendees` inner join `bookings` on `attendees`.`id` = `bookings`.`attendee_id` where `bookings`.`scheduled_program_segment_id` = '1412' and `bookings`.`registered` = '1'430μs
select count(*) as aggregate from `attendees` inner join `bookings` on `attendees`.`id` = `bookings`.`attendee_id` where `bookings`.`scheduled_program_segment_id` = '1413' and `bookings`.`registered` = '1'

编辑

控制器中的即时加载

$programs = ScheduledProgram::with('scheduledProgramSegments.registeredAttendees','scheduledProgramSegments.attendees')->where('registration_start_date', '<=', $today)
                            ->where('end_date',                '>=',  $today)
                            ->get();

$user->load('attendees', 'attendees.scheduledProgramSegments')->get();



return View::make('user/registration/edit', compact('user','programs'));

调度程序模型

     public function scheduledProgramSegments()
  {
    return $this->hasMany('ScheduledProgramSegment');
  }
  public function program()
  {
    return $this->belongsTo('Program');
  }
  public function programSession()
  {
    return $this->belongsTo('ProgramSession');
  }
  public function attendees()
  {
    return $this->belongsToMany('Attendee', 'prog_bookings')->withPivot('registered','paid')->withTimestamps();
    //return $this->hasManyThrough('Attendee', 'ScheduledProgramSegment','id','id' );
  }

  public function registeredAttendees()
  {
      //return $this->scheduledProgramSegments->attendees()->wherePivot('registered', 1);
      return $this->attendees()->wherePivot('registered', 1);
  }
  public function getRegisteredCountAttribute()
  {
    //return $this->registeredAttendees->count();
    //Consider changing this to calculate the max registered of the child programs
    $largest = 0;
    $segments = $this->scheduledProgramSegments()->get();
//return $segments->first()->registered_count;
    foreach($segments as $segment){
      $largest = ($segment->registered_count > $largest) ? $segment->registered_count : $largest;
    }
    return $largest;
  }

与会者模型

public function user()
{
    return $this->belongsTo('User');
}

public function scheduledProgramSegments()
{
    return $this->belongsToMany('ScheduledProgramSegment', 'bookings')->withPivot('paid','registered')->withTimestamps();
}

看法

  <td class="cap">{{{$program->registered_count . '/' . $program->max_attendees}}}</td>

共1个答案

匿名用户

将getRegisteredCountAttribute函数更改为

 public function getRegisteredCountAttribute()
 {
      //return $this->registeredAttendees->count();
      //Consider changing this to calculate the max registered of the child programs
      $largest = 0;
      $segments = $this->with('scheduledProgramSegments')->get(); // this is the line to change. this will prevent the N+1 query problem.

      //return $segments->first()->registered_count;
      foreach($segments as $segment){
           $largest = ($segment->registered_count > $largest) ? $segment->registered_count : $largest;
      }

      return $largest;

}

还有这个

 public function registeredAttendees()
 {
    //return $this->scheduledProgramSegments->attendees()->wherePivot('registered', 1);
    return $this->with('attendees')->wherePivot('registered', 1);
 }