Skip to content
Advertisement

The most efficient way to do eagerload in Laravel Policy for yajra-datatable use

I need to eager load relationship within laravel policy The problem is i use the laravel policy inside yajra-datatable which will load it (the policies) line by line

Here’s the code looks like :

JavaScript

and i use it in my yajra-datatable like so :

JavaScript

so every line will load the relation again and again

I’m expecting more efficient way to load them just once instead of load the relation line by line How can i achieve this ?

Update :

I tried to use accessor on User model to append the relations with sections table

JavaScript

This was success for only the FIRST relation only, i tried to remove the first() method but got the error PDO serialization instead

JavaScript

Advertisement

Answer

I think Laravel policies uses the exact object it is called on. So $this->user->can(…) actually is the same object that is being passed as first parameter to the create(User $user) policy method.

In that case, I would try to load it before you call the ->can method inside the closure.

The code could look like this:

JavaScript

And then you also have to remember to remove the $user->load('sections'); from inside the create() method in the policy. This is very important 🙂

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