I have used foreign key there a little but, try this I used a bit.
Firs use Eloquent
For example I have this, my models of Users and Profiles and a User has 1 or more profiles and I referenced them in their respective model.
So for example:
$user=User::find($id);
$user->profile()->get();
And there I obtain all the profiles of the user.
Be sure de profile() method/function is the name of the function that you named example
public function profile() { $this->belongsTo('User'); }
something like this
Hi araceus,
Thank you for your answer, but it's not quite what i am looking for. If you look at the model i have the relations setup. But my problem is querying from toolcategory->tools->tooltype and then only getting the categories that is related to existing tools that is of a specific tooltype. Hope it is clear, but it is a bit hard to explain, properbly to understand too:)
Try doing the search from the ToolType model and then chaining the needed relations from there.
Abbajbryant,
Could you give an example of how you would do that, i am not sure what you mean.
I got i working.
The keys where switched in
return $this->belongsToMany('Tool', 'tool_toolcategory', 'tool_id', 'toolcategory_id');
and i had to change the scope to
public function scopeHasType($query, $type)
{
return $query->whereHas('tools', function ($q) use ($type)
{
$q->whereHas('tooltype', function($q) use ($type)
{
$q->where('tooltype.name', '=', $type);
});
})
}
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community