I have two models Scholarship and Level, and they have many to many relationship. And for this I have two tables on my database as scholarships and levels, and a pivot table level_scholarship. I want to make a query for getting my scholarships based on scholarship (title, app_deadline) and level (name)
Columns:
My levels table have: id, name, slug, created_at, updated_at
My scholarships table have: id, title, slug, description, image, university, app_deadline, deadline, remarks, place_id, created_at, updated_at
And level_scholarship have: id, scholarship_id, level_id, created_at, updated_at
My Scholarship Model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Scholarship extends Model
{
public function levels(){
return $this->belongsToMany('App\Level')->withTimestamps();
}
}
My Level Model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Level extends Model
{
public function scholarships(){
return $this->belongsToMany('App\Scholarship')->withTimestamps();
}
}
I can make query for Scholarship model, but how do I relate Level Model
$query = $request->input('query');
$field = Field::where('title','LIKE',"%$query%")->first();
$fieldId = $field->id;
$scholarships = $field->scholarships()->latest()->paginate(10);;
$scholarship = Scholarship::where('title','LIKE',"%$query%")
->orWhere('app_deadline','LIKE',"%$query%")
->orWhere(scholarship()->levels()->name,'LIKE',"%$query%")
->latest()->get();