I have to display a list of transportations in such way:
Entity "transportation" can have many transportation items. I use Laravel framework and yajra/laravel-datatables-oracle package. List is being displayed, but datatables built-in AJAX search function does not work properly: it does not search on edited table rows "sender_address" and "delivery_address" where I concatenate all addresses of senders and all addresses of recipients.
Here is the code:
<?php
namespace Modules\Transportations\Http\Controllers;
use Modules\Transportations\Entities\Transportation;
use Yajra\DataTables\Facades\DataTables;
use Yajra\DataTables\Services\DataTable;
class DataTablesTransportationsController extends DataTable
{
public function query()
{
$query = Transportation::query()->with([
'transportationItems'
]);
// closure for transportationItems does not work at all:
$query = DataTables::of($query)->filter(function ($queryInner) {
if (request()->has('search') && isset(request()->input('search')['value']) && !empty(request()->input('search')['value'])) {
$search_term = request()->input('search')['value'];
$queryInner->where('sender_address', 'LIKE', "%".$search_term."%");
}
});
return $query;
}
// editing columns of data table:
public function ajax()
{
$dataTablesObj = DataTables::eloquent($this->query())
->editColumn('sender_address', function($transportation){
$dataToImplode = [];
foreach ($transportation->transportationItems as $item) {
$dataToImplode[] = $item->sender_address;
}
return implode('<br/>', $dataToImplode);
})
->editColumn('delivery_address', function($transportation){
$dataToImplode = [];
foreach ($transportation->transportationItems as $item) {
$dataToImplode[] = $item->recipient_address;
}
return implode('<br/>', $dataToImplode);
});
return $dataTablesObj->make(true);
}
public function html()
{
/* if I remove attributes "'searchable' => false",
DataTables throws error, for example: "unknown column
'transportations.sender_address'" */
return $this->builder()
->columns([
[ 'data' => 'id', 'name' => 'transportations.id', 'title' => '<input type="checkbox" class="check-all" />', 'width' => 30, 'orderable' => false ],
[ 'data' => 'order_number', 'name' => 'order_number', 'title' => 'Nr.' ],
[ 'data' => 'sender_address', 'name' => 'sender_address', 'title' => 'Sender address', 'orderable' => false, 'searchable' => false ],
[ 'data' => 'delivery_address', 'name' => 'delivery_address', 'title' => 'Recipient address', 'orderable' => false, 'searchable' => false ],
])
->minifiedAjax();
}
}
/* and here is my main Transportations CRUD controller,
where I injected DataTablesTransportationsController object into index
method. It generates the table of transportations:
*/
namespace Modules\Transportations\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller;
use Modules\Transportations\Entities\Transportation;
class TransportationsController extends Controller
{
public function index( DataTablesTransportationsController $dataTable )
{
return $dataTable->render('transportations::index');
}
/* other methods ("actions") are here */
}
?>
So what should I do and how to make that search function work? I know it looks silly at first glance: transportation may have many transportation items, so data of transportation items should not be displayed in transportations list. On the other hand, I know that there could be only a few transportation items for each transportation in real world.
What could be different solutions to solve this problem? To make an list of transportation items? It should be negotiated with my manager, then.