1

This is loop inside my controller:

 foreach(Classes::all() as $classes){
        $classes->student;
    }
    return view('configuration.configuration')->with('classes', $classes);

My question is how to make an object that will fill itself in each passing, so that I can pass that object to view.

2
  • $classes->student; has no effet, what are you trying to do exaclty? Commented Aug 2, 2016 at 8:25
  • You want to load the student with the each $classe ? Commented Aug 2, 2016 at 8:26

1 Answer 1

2

To eager load the students of class, with no need to pars all classes over a loop, you can do :

$classes = Classes::with('students')->all();

Assuming that you have a "one-to-many" relationship like this in your Classes model:

public function students()
{
    return $this->hasMany('App\Student');
}
Sign up to request clarification or add additional context in comments.

1 Comment

thank you... I'm still wraping my head around eager loading and eloquent

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.