Skip to content
Advertisement

Laravel 7 not finding with scopes

I have a model called Shift on my application, and I’ve defined my relationships and scopes on it like this:

Relationship:

JavaScript

With scope:

JavaScript

Now, when I retrieve shifts and try to call these scopes, I do the following:

JavaScript

However, I get this error…

JavaScript

I’m not sure why this is happening? It should pick up the scope shouldn’t it?

Advertisement

Answer

when you call Shift::all() you get all the shifts table record from db as a collection , then you load the relation on that collection which makes that error.

you should not be loading the result from db unless your query is ready, you should tell the query builder to load the relation then call the result:

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