MedicOneSystems / livewire-datatables

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

Searchable not working when added to a column from a relationship #573

Closed dotmarn closed 11 months ago

dotmarn commented 1 year ago

public function builder() { return EmployeeDetail::query()->with('user')->where('company_id', 1); }

` public function columns() { return [ Column::name('user.username') ->defaultSort('asc') ->searchable() ->label('Staff ID')->unwrap(),

        Column::raw("CONCAT(firstname, ' ', lastname) AS name")
            ->searchable()
            ->label('Name')->unwrap(),
    ];
}`

It gives the error below

image
dotmarn commented 11 months ago

I solved this by using using the joins instead of relationship.

emresasi commented 7 months ago

Can you tell me exactly how you solved it? That's exactly what I'm trying to do. My deals table has a morphToMany relationship with my contacts table. I am trying to combine the contact name and surname, but I am getting an error. This process shouldn't be this difficult.

dotmarn commented 7 months ago

@emresasi I used leftJoin for the tables involved instead of using relationship and the search didn't break.

emresasi commented 7 months ago

@dotmarn I understand that that process will not work for me because a deal has more than one contact, so I want to show your transaction collectively in that column without using lefjoin

 Column::raw('CONCAT(users.name," ",users.surname) AS userName')
                ->label(__('User'))
                ->defaultSort('asc')
                ->filterable(),

You should be able to solve it like this :) My process only gives count in this way, so I can't solve it anyway. I haven't found a solution for days.


Column::name('contacts.name,surname')
                 ->label(__('Contacts'))
                 ->view('components.datatable.deal_contacts')
                 ->filterable(),

In this process, the name and surname are combined in this way. 1

I convert it to this shape by using str in view. 2

<div>
    <ul style="list-style: none">
        @forelse(explode(',',$value) as $contact)
            @if($contact)
            <li><span class="badge badge-primary"
                      >{{\Illuminate\Support\Str::headline(\Illuminate\Support\Str::studly($contact))}}</span></li>
            @endif
        @empty
            -
        @endforelse
    </ul>
</div>

But I continue to research in case there is an easier way :)

I'll put it here in case it might be useful to someone else :)