I have 'restaurant' and 'working_hours' table,
restaurant -> hasMany -> working_hours
working_hours -> belongsTo -> restaurant
What I want is to show few columns from restaurant ( res_name, res_address and res_location ) together side by side with the table columns of working_hours, but am not sure how to do this
Currently this is how my res_working_hourDataTable.php look like:
<?php
namespace App\DataTables;
use App\Models\res_working_hour;
use App\Models\restaurant;
use Form;
use Yajra\Datatables\Services\DataTable;
class res_working_hourDataTable extends DataTable
{
/**
* @return \Illuminate\Http\JsonResponse
*/
public function ajax()
{
return $this->datatables
->eloquent($this->query())
->addColumn('action', 'res_working_hours.datatables_actions')
->make(true);
}
/**
* Get the query object to be processed by datatables.
*
* @return \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder
*/
public function query()
{
// $test = restaurant::with('res_working_hour')->select('restaurants.*');
//
// return $this->applyScopes($test);
$resWorkingHours = res_working_hour::query();
return $this->applyScopes($resWorkingHours);
}
/**
* Optional method if you want to use html builder.
*
* @return \Yajra\Datatables\Html\Builder
*/
public function html()
{
return $this->builder()
->columns($this->getColumns())
->addAction(['width' => '10%'])
->ajax('')
->parameters([
'dom' => 'Bfrtip',
'scrollX' => false,
'buttons' => [
'print',
'reset',
'reload',
[
'extend' => 'collection',
'text' => '<i class="fa fa-download"></i> Export',
'buttons' => [
'csv',
'excel',
'pdf',
],
],
'colvis'
]
]);
}
/**
* Get columns.
*
* @return array
*/
private function getColumns()
{
return [
// 'res name' => ['name' => 'restaurant.res_name', 'data' => 'restaurant.res_name'],
// 'res address' => ['name' => 'restaurant.res_address', 'data' => 'restaurant.res_address'],
// 'res location' => ['name' => 'restaurant.res_location', 'data' => 'restaurant.res_location'],
'res_id' => ['name' => 'res_id', 'data' => 'res_id'],
'day' => ['name' => 'day', 'data' => 'day'],
'working_hour' => ['name' => 'working_hour', 'data' => 'working_hour']
];
}
/**
* Get filename for export.
*
* @return string
*/
protected function filename()
{
return 'resWorkingHours';
}
}
Most helpful comment
See this demo for ref?