Query that has a where clause and with clause.

Posted 1 year ago by jhutto

I'm writing a ajax call to a controller with a function to return $courses I have two tables/Models... Courses and Instructors.

Instructor has...

public function Courses()
        return $this->hasMany('App\Course');

Courses has...
public function Instructor()
        return $this->belongsTo('App\Instructor');

I have a query that's working but would like to know how to write it the eloquent way...

This works. $courses = \App\Course::where('SemesterID', $id)->join('Instructors', 'somascourses.Instructor_id', '=', 'Instructors.id')->get();

This gives me an error not understanding the ::with..... //$courses = \App\Course::where('SemesterID', $id)::with('Instructor')->get();

Sorry... A couple of more questions.

  1. Should I be writing these as functions in the Model ? Or is it ok in the controller?

  2. How do I bring over only specific fields from the instructors table in the query?

Please sign in or create an account to participate in this conversation.