I've got two models: Offers and Speciality with many to many relatioship:
in Offer.php
public function specialities()
{
return $this->belongsToMany('App\Speciality');
}
in Speciality.php
public function offers()
{
return $this->belongsToMany(Offer::class);
}
this is simple so far.
Now, I'm building query to get only Offers with certain speciality. The problem is that I'm using when helper function, so I don't know how to access speciality model:
public function template($person = "", $speciality = "")
{
$offers = Offer::when($speciality, function($query, $speciality) {
return $query->specialities()->where('speciality_name', $speciality)->get();
})->orderBy('created_at','desc')->paginate(9);
}
And this is not working for me. Can anyone give me a tip how should I write it?