laravel-livewire-tables icon indicating copy to clipboard operation
laravel-livewire-tables copied to clipboard

CONCAT two columns ?

Open mvpopuk opened this issue 4 years ago • 8 comments

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(),

mvpopuk avatar Apr 29 '20 18:04 mvpopuk

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(),

simonridley avatar May 04 '20 09:05 simonridley

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 avatar May 09 '20 21:05 mvpopuk

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 avatar May 10 '20 12:05 mvpopuk

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

mvpopuk avatar May 11 '20 07:05 mvpopuk

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.

Sir-Nigel avatar Nov 04 '20 23:11 Sir-Nigel

Sorting has this same issue

HelDoRe avatar Nov 23 '20 23:11 HelDoRe

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]); }), '

ranidentity avatar Jan 28 '21 08:01 ranidentity

Thank you..

From: ranidentity [email protected] Reply to: kdion4891/laravel-livewire-tables [email protected] Date: Thursday, 28 January 2021 at 10:34 To: kdion4891/laravel-livewire-tables [email protected] Cc: Sir-Nigel [email protected], Comment [email protected] 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.

Sir-Nigel avatar Jan 28 '21 09:01 Sir-Nigel