Reputation: 1952
I'm null in SQL.
I have two tables works
and types
.
works
contain a foreign_key type_id
which references a work type (in my DB, this is the ID of a type).
type
contain an ID
, name
and slug
.
I would like to get all works from a particular type. Example: Get all works with the type website
(website is the slug).
My relation for my model Work
public function type()
{
return $this->belongsTo('App\Models\Type');
}
My relation for my model Type
public function works()
{
return $this->hasMany('App\Models\Work');
}
I tried this but it's totaly wrong
Work::with('types')->where('slug', $request->get('type'));
Thank you !
Upvotes: 2
Views: 141
Reputation: 11906
Your relationship is defined as type
and you're loading types
.
$works = Work::whereHas('type', function ($query) use ($request) {
$query->where('slug', $request->get('type'));
})->get();
Upvotes: 5