6

I'm having trouble on the eager loading. Let's say I have models of Members, TrainingCategory, TrainingCategoryResult and Registration

Member Model:

public function registration() {
    return $this->hasMany('Registration', 'member_id');
}
public function trainingResults(){
    return $this->hasMany('trainingResult', 'member_id');
}
public function trainingCategoryResults() {
  return $this->hasMany('TrainingCategoryResult', 'member_id');
}

TrainingCategory Model:

public function trainings() {
    return $this->hasMany('Training', 'id');
}
public function trainingCategoryResults() {
    return $this->hasMany('trainingCategoryResult', 'category_id');
}

TraningCategoryResult Model:

  public function category() {
        return $this->belongsTo('TrainingCategory', 'id');
  }
  public function member() {
        return $this->belongsTo('Member', 'id');
  }

Registration Model:

  public function course() {
    return $this->belongsTo('Course', 'course_id');
  }
  public function member() {
    return $this->belongsTo('Member', 'id');
  }

I am trying to eager load all the registration info and its related info including the TraningCategoryResult info but I not sure how to get that TraningCategoryResult which required two foreign keys (category_id and member_id), is there any way to do that?

Here is my code atm:

 $members= Member::where(function($query) use ($id, $site) {
    $query
    ->where('id', '=', $id)
    ->where('site', '=', $site);
  });

 $members= $members
  ->with('registration.course',
  'registration.course.traningCategories',
  ->get(['member.id']);

Thank you.

Theren
  • 383
  • 1
  • 3
  • 9
  • I'm not sure whether I understand this properly, but why can't you just join? something like Member::with('categoryResult')->with('registration')->get() or Member::with(array('categoryResult', 'registration'))->get() ( its pseudo code, not sure how it's going to work ) – mmmm May 30 '15 at 00:15
  • Does this works? `Registration::with('member.trainingCategoryResults')->get();` – Torgheh May 05 '17 at 09:40

2 Answers2

0

This will not work Member::with('categoryResult')->with('registration')->get()

You can make a new relation in Member Model

public function categoryResult()
{
    return $this->belongsTo('Category')->with('Registration');
}

//and then call

Member::with('categoryResult')->get();

0

You could use a few options to achieve that:

OPTION 1: create a variable relationship

Change your relation in the Member model

public function trainingCategoryResults($category_id = null) {
  if(empty($category_id))
    return $this->hasMany('TrainingCategoryResult', 'member_id');
  else
    return $this->hasMany('TrainingCategoryResult', 'member_id')
      ->where('category_id', $category_id);
}

The code above might have limitations and it doesn't take advantage of many laravel features, but it will work.

OPTION 2: Access from relationship

You can keep everything as is, and load as follow:

$members= Member::where(function($query) use ($id, $site) {
  $query
    ->where('id', '=', $id)
    ->where('site', '=', $site);
})
->where('id', $member_id) // set the id of the memeber
->with(array(
  'traningCategoryResults' => function($q)use($category_id){
    $q->where('category_id', $category_id); // This makes sure you get only desired results
  }
))

In this way you will have only what you need, assuming you know the $category_id

clod986
  • 2,527
  • 6
  • 28
  • 52