lundi 14 mars 2016

Search the database laravel Elquent

I am trying to search the database I have these Models Centers has Many Courses and Courses Has Many Subjectarea their is pivot table between the Models Courses & Subjectarea

I Want to be able to Get the Center With his courses Where specific Subjectarea here is the code for the search method

$keyword =$request->input('subjectareas');
$level=$request->input('levels');
$centers = Center::with('courses')
  ->whereHas('courses', function ($query) use ($level) {
        $query->where('studylevel_id',$level);
 })->whereHas('courses.subjectareas', function ($query) use ($keyword)
   { 
      $query->where('subjectarea_id',$keyword );
  })
->get();

class Course extends Model

{
 public function subjectareas() {

 return $this->belongsToMany('App\Subjectarea')->withTimestamps();

} 
}

 class Subjectarea extends Model{ 
public function courses()
{

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

 }
 }

 class Center extends Model {
  public function courses() {

return $this->hasMany('App\Course')->withTrashed(); 

    } 
 }



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire