Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 2 years ago.
0

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

Last updated 2 years ago.
0

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:)

Last updated 2 years ago.
0

Try doing the search from the ToolType model and then chaining the needed relations from there.

Last updated 2 years ago.
0

Abbajbryant,

Could you give an example of how you would do that, i am not sure what you mean.

Last updated 2 years ago.
0

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);
          });
      })
}
Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

RuneLyng runelyng Joined 14 Jul 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.