MedicOneSystems / livewire-datatables

Advanced datatables using Laravel, Livewire, Tailwind CSS and Alpine JS
https://livewire-datatables.com/
MIT License
1.19k stars 259 forks source link

issue in search when using 'with' method #600

Closed merngalaxyprogs closed 11 months ago

merngalaxyprogs commented 11 months ago

Hi !

When i use the with method like this

public function builder()
    {
        return Profile::with('user');
    }

I get an issue when searching

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'profiles.firstname' in 'where clause'

But when i use the left join method like in the examples the search works fine

Nowocyn commented 11 months ago

It is a normal behavior of Laravel. As an example, if you try the following code somewhere else in your Laravel app it will also return the error:

Profile::query()->with('user')->where('users.firstname')->get();
// Or
Profile::query()->with('user')->where('user.firstname')->get();

Or

Profile::query()->with('user')->addSelect('users.firstname')->get();
// Or
Profile::query()->with('user')->addSelect('user.firstname')->get();

The function with only eager loads the given relationships but does not inject any joins into your query. That means Laravel will load all models from your database and all relationships will be loaded in a secondary query. You can check this behavior with telescope from Laravel or some other packages.

merngalaxyprogs commented 11 months ago

Yes of course i totally forgot this point, displaying data is a thing and searching with relationships is another thing. Thanks a lot

Nowocyn commented 11 months ago

I also went into this error on my first usage with this package.

Could you close your issue? :)

merngalaxyprogs commented 11 months ago

Of course bro thanks :)