kdion4891 / laravel-livewire-tables

A dynamic, responsive Laravel Livewire table component with searching, sorting, checkboxes, and pagination.
302 stars 41 forks source link

CONCAT two columns ? #21

Open mvpopuk opened 4 years ago

mvpopuk commented 4 years ago

Hi, awesome package. I love it. If is possible I would like to sponsor it.

Anyways, is there a way to CONCAT two db columns like FirstName and LastName ?

Column::make('First Name', 'FirstName')->searchable()->sortable(),
Column::make('Last Name', 'LastName')->searchable()->sortable(),
simonridley commented 4 years ago

You can do this by adding an Accessor to the Model, then appending it so that it's available to use as any other column

For example, on the Model:

public function getFullNameAttribute() { return $this->first_name . ' ' . $this->last_name; }

protected $appends = [ 'full_name' ];

And then in the TableComponent you can use this like so:

Column::make('FullName')->searchable()->sortable(),

mvpopuk commented 4 years ago

Now I have a problem with FullName when searching:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'customers.FullName' in 'where clause' (SQL: select count(*) as aggregate from `customers` where (`customers`.`id` like %bobo% or `customers`.`FullName` like %bobo% or `customers`.`phone` like %bobo% or `customers`.`mobile` like %bobo% or `customers`.`city` like %bobo% or `customers`.`postcode` like %bobo% or `customers`.`created_at` like %bobo%) and `customers`.`deleted_at` is null)

mvpopuk commented 4 years ago

I think I have to extend or modify the query logic since I only want to search if the CONCAT FirstName and LastName are equal with the input query

mvpopuk commented 4 years ago

I will try to CONCAT $column . $column see what happens :)

Sir-Nigel commented 3 years ago

Hi Guys i have been having the same issue, have tried everything above but still getting an error if i try to search by full_name.

HelDoRe commented 3 years ago

Sorting has this same issue

ranidentity commented 3 years ago

i tried something like this and it work User::query()->selectRaw("*, concat('first_name','last_name') as full_name"); but sorting is buggy, it sort only once which i don't understand why ' Column::make('Display Name','full_name')->sortable()->sortUsing(function ($models, $sort_attribute, $sort_direction) { return $models->orderByRaw('full_name',[$sort_attribute, $sort_direction]); }), '

Sir-Nigel commented 3 years ago

Thank you..

From: ranidentity notifications@github.com Reply to: kdion4891/laravel-livewire-tables reply@reply.github.com Date: Thursday, 28 January 2021 at 10:34 To: kdion4891/laravel-livewire-tables laravel-livewire-tables@noreply.github.com Cc: Sir-Nigel nigel@leadingdigital.africa, Comment comment@noreply.github.com Subject: Re: [kdion4891/laravel-livewire-tables] CONCAT two columns ? (#21)

i tried something like this and it work User::query()->selectRaw("*, concat('first_name','last_name') as full_name"); but sorting is buggy, it sort only once which i don't understand why 'Column::make('Display Name','full_name')->sortable()->sortUsing(function ($models, $sort_attribute, $sort_direction) { return $models->orderByRaw('full_name',[$sort_attribute, $sort_direction]); }),'

— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or unsubscribe.