Skip to content
Advertisement

Laravel eloquent how to get relationships relationship?

I have Users table,Jobs table and JobStatus table. I get user jobs using relationship

JavaScript

This will return all user jobs as collection, not as relationship inside user model. Now I need to get jobs statuses, but when I try this

JavaScript

I get error Call to undefined method. I need to get statuses as collection so I can use where,sort by etc while getting them from db. Is there any way to do so?

I need to do something like this , but with statuses

JavaScript

Advertisement

Answer

To retrieve the jobs by filtering on the statuses relationship, you can do it like this:

JavaScript

For more information about querying relationship existence: https://laravel.com/docs/7.x/eloquent-relationships#querying-relationship-existence


If you want to retrieve the statuses instances, you can do it like this:

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement