Search by all the DB columns instead of by only the columns shown in the frontend
Search by all the DB columns instead of by only the columns shown in the frontend

So I want to search for all the table fields instead of only the columns showed in the frontend. As you can see here in the backend i return all of the model fields (and adding 3 more):
$properties = Property::with(['creator', 'assignedUser', 'owner'])
->select('properties.*',
'creator.name as creator_name',
'assigned_user.name as assigned_user_name',
'owner.name as owner_name')
->leftJoin('users as creator', 'creator.id', '=', 'properties.created_by')
->leftJoin('users as assigned_user', 'assigned_user.id', '=', 'properties.assigned_to')
->leftJoin('owners as owner', 'owner.id', '=', 'properties.owner_id');
$properties->filterByPrice($minPrice, $maxPrice)
->filterByArea($minArea, $maxArea)
->filterByStatus($status)
->filterByBedrooms($bedrooms)
->filterByBathrooms($bathrooms);
return DataTables::eloquent($properties)
->addColumn('actions', function (Property $property) {
return $property->id;
})
->filterColumn('creator_name', function ($query, $keyword) {
$query->whereRaw('creator.name like ?', ["%{$keyword}%"]);
})
->filterColumn('assigned_user_name', function ($query, $keyword) {
$query->whereRaw('assigned_user.name like ?', ["%{$keyword}%"]);
})
->filterColumn('owner_name', function ($query, $keyword) {
$query->whereRaw('owner.name like ?', ["%{$keyword}%"]);
})
->orderColumn('creator_name', 'creator.name $1')
->orderColumn('assigned_user_name', 'assigned_user.name $1')
->orderColumn('owner_name', 'owner.name $1')
->make(true);
And in the frontend i show only this fields:
const columns = ref([
{ title: 'ID', data: 'id', name: 'id' },
{ title: 'Título', data: 'title', name: 'title' },
{
title: 'Precio',
data: 'price',
name: 'price',
render: (price) => new Intl.NumberFormat('es-ES', { style: 'currency', currency: 'EUR' }).format(price)
},
{ title: 'Estado', data: 'status', name: 'status', },
{ title: 'Area', data: 'area', name: 'area', render: (data) => `${data}/m2` },
{
title: 'Propietario',
data: 'owner',
name: 'owner_name',
render: '#owner',
visible: props.showOwner
},
{
title: 'Creado Por',
data: 'creator',
name: 'creator_name',
render: '#creator'
},
{
title: 'Asignado a',
data: 'assigned_user',
name: 'assigned_user_name',
render: '#assigned_user',
visible: props.showAssignedTo
},
{ title: 'Acción', data: 'actions', name: 'actions', orderable: false, searchable: false, render: '#action' },
]);
I didnt find any option in the documentation to make this happen (search by all the table fields).
Replies
I don't know what server-side processing script you are using there, it isn't one that is published as part of the DataTables project, but what you could perhaps do is define the fields (
addColumn
?) but just not display them on the client-side. Or if the script doesn't allow for that, make the columns you don't want show hidden on the client-side.Allan
Sorry i forgot to mencione it, im using https://github.com/yajra/laravel-datatables for the server side processing.
"Or if the script doesn't allow for that, make the columns you don't want show hidden on the client-side."
I was gonna do that but i thought that should be an easier and cleaner way to do it.
You could try asking yajra if extra columns can be added - the author is very helpful.
From a DataTables point of view, just because the server-side sends JSON with a particular property, it doesn't mean you have to have a column showing it. So defining an extra column should be all you need I would think.
Allan