Laravel-datatables: How to send parameters to DataTable Service UsersDataTable Class?

Created on 15 Mar 2017  ·  11Comments  ·  Source: yajra/laravel-datatables

How do I pass parameters ( user_id, username etc ) to the DataTable Service UsersDataTable Class for DB queries?

UsersController:

class UsersController extends Controller
{
    public function index(UsersDataTable $dataTable)
    {
        return $dataTable->render('users');
    }
}

Do I need to customize the functions in UsersDataTable Class like public function query($param) {} or public function ajax($param) {}?

documentation enhancement question

Most helpful comment

If you are using v7, you can use:

class UsersController extends Controller
{
    public function index(UsersDataTable $dataTable)
    {
        return $dataTable->with('key', 'value')->render('users');
    }
}

And access it like a property on dataTable class.

public function query() {
  $key = $this->key;
  ...
}

All 11 comments

If you are using v7, you can use:

class UsersController extends Controller
{
    public function index(UsersDataTable $dataTable)
    {
        return $dataTable->with('key', 'value')->render('users');
    }
}

And access it like a property on dataTable class.

public function query() {
  $key = $this->key;
  ...
}

This is the commit where I added this functionality in case you need. Not yet added on documentation.

hi, thank you for the reply, I am currently using 6.x for Laravel 5.3.30,

will it work if I copy and paste the commit code into datatables 6.x?

I added the commit code to the Datatables 6.x package, it is working nicely just that in the DataTable Service class query():

    public function query()
    {

        $id = $this->id;

        $restaurant = restaurant::where('res_id', $id);

        return $this->applyScopes($restaurant);

    }

the return $this->applyScopes($restaurant); , $restaurant is showing a warning:

Expected \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder, got \App\Models\restaurant less... (Ctrl+F1) 
Invocation parameter types are not compatible with declared.

but If I add the ->get() like, return $this->applyScopes($restaurant->get()); the warning will be gone, but the datatable View will get error like:

DataTables warning: table id=dataTableBuilder - ajax error.

http://prntscr.com/el53kh

Can you paste your restaurant class? I think there is something wrong with your model.

Anyways, it is not advisable to edit the vendor folder so I decided to add the fluent variables on v6 too.

Please update to v6.27.0 to have this feature. Thanks!

hi, this is my current restaurant Model Class:

<?php

namespace App\Models;

use Eloquent as Model;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * Class restaurant
 *
 * @package App\Models
 * @version February 27, 2017, 5:13 pm UTC
 * @property int $res_id
 * @property string $res_name
 * @property string $res_image
 * @property string $res_image_date
 * @property string $res_address
 * @property string $res_state
 * @property string $res_location
 * @property string $res_area
 * @property string $res_telephone
 * @property int $res_min_price
 * @property int $res_max_price
 * @property float $res_latitude
 * @property float $res_longitude
 * @property bool $res_chain
 * @property bool $res_new
 * @property bool $res_offer
 * @property bool $res_type_state
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\res_working_hour[] $workingHours
 * @method static \Illuminate\Database\Query\Builder|\App\Models\restaurant whereResAddress($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\restaurant whereResArea($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\restaurant whereResChain($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\restaurant whereResId($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\restaurant whereResImage($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\restaurant whereResImageDate($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\restaurant whereResLatitude($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\restaurant whereResLocation($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\restaurant whereResLongitude($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\restaurant whereResMaxPrice($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\restaurant whereResMinPrice($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\restaurant whereResName($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\restaurant whereResNew($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\restaurant whereResOffer($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\restaurant whereResState($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\restaurant whereResTelephone($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\restaurant whereResTypeState($value)
 * @mixin \Eloquent
 */
class restaurant extends Model
{
    //use SoftDeletes;

    public $table = 'fc_restaurant';
    public $timestamps = false;

    //const CREATED_AT = 'created_at';
    //const UPDATED_AT = 'updated_at';


    protected $dates = ['deleted_at'];

    protected $primaryKey = 'res_id';
    //protected $hidden = 'res_id';

    public $fillable = [
        'res_name',
        'res_image',
//        'res_image_date',
        'res_address',
        'res_state',
        'res_location',
        'res_area',
        'res_telephone',
        'res_min_price',
        'res_max_price',
        'res_latitude',
        'res_longitude',
        'res_chain',
        'res_new',
        'res_offer',
        'res_type_state'
    ];

    /**
     * The attributes that should be casted to native types.
     *
     * @var array
     */
//    protected $casts = [
//        'res_id' => 'integer',
//        'res_name' => 'string',
//        'res_image' => 'string',
//        'res_address' => 'string',
//        'res_state' => 'string',
//        'res_location' => 'string',
//        'res_area' => 'string',
//        'res_telephone' => 'string',
//        'res_min_price' => 'integer',
//        'res_max_price' => 'integer',
//        'res_latitude' => 'float',
//        'res_longitude' => 'float'
//    ];

    /**
     * Validation rules
     *
     * @var array
     */
    public static $rules = [

    ];


    public function workingHours()
    {
        return $this->hasMany('App\Models\res_working_hour', 'res_id');
    }


}

Hey yajra, It's $key = $this->attributes; not $key = $this->key; Right?
I used this $key = $this->attributes;

@jcduenasr the class uses a magic getter method which fetches the values from keys in the attributes.

Hi, I have two date fields in a form to filter the table query by dates and prevent it from bringing me thousands of records.
The problem is that when I try to export with one of the buttons, the query is not sending the date filters, so I'm not bringing anything.
How do I send the date fields using the buttons to export?

This is my class.
namespace AppDataTables;
use App\Accesos;
use YajraDatatables\Services\DataTable;

class AccesosDataTable extends DataTable
{

public function ajax()
{   

    return $this->datatables
    ->eloquent($this->query())
    ->make(true);

}


public function html()
{
    return $this->builder()
                ->columns($this->getColumns())
                    ->ajax('')
                ->parameters([
                    'dom'          => 'Bfrtip',  
                    'buttons'      => ['export', 'reload'],
                ]);
}

public function query()
{
    $key = $this->attributes;
    $users = Accesos::whereBetween('field_filter',[$key['from'].' 00:00:00',$key['to'].' 23:59:59'])->select();

    return $this->applyScopes($users);

}

protected function getColumns()
{
    return [
        'field1',
        'field2',
        'field3',
        'field4',
        'field5',
        'field6',
    ];
}

    protected function filename()
    {
         return strtolower(trans('accesos'));
    }

}

Was this page helpful?
0 / 5 - 0 ratings

Related issues

t0n1zz picture t0n1zz  ·  3Comments

vipin733 picture vipin733  ·  3Comments

FilipeBorges1993 picture FilipeBorges1993  ·  3Comments

josiahke picture josiahke  ·  3Comments

SGarridoDev picture SGarridoDev  ·  3Comments