Laravel-datatables: Showing table columns data from 'belongsTo' relationship together with table data

Created on 11 Mar 2017  路  2Comments  路  Source: yajra/laravel-datatables

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';
    }
}

question

Most helpful comment

See this demo for ref?

All 2 comments

See this demo for ref?

See this tutorial here

Was this page helpful?
0 / 5 - 0 ratings