Laravel-datatables: 如何将参数发送到DataTable服务UsersDataTable类?

创建于 2017-03-15  ·  11评论  ·  资料来源: yajra/laravel-datatables

如何将参数(user_id,username等)传递给DataTable Service UsersDataTable类以进行数据库查询?

UsersController:

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

我是否需要自定义UsersDataTable类中的函数,例如public function query($param) {}public function ajax($param) {}

documentation enhancement question

最有用的评论

如果您使用的是v7,则可以使用:

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

并像访问dataTable类的属性一样访问它。

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

所有11条评论

如果您使用的是v7,则可以使用:

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

并像访问dataTable类的属性一样访问它。

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

这是我在需要时添加此功能的提交。 尚未在文档中添加。

嗨,谢谢您的答复,我目前在Laravel 5.3.30中使用6.x,

如果我将提交代码复制并粘贴到数据表6.x中,它将起作用吗?

我将提交代码添加到了Datatables 6.x包中,它在DataTable Service类query()很好地工作

    public function query()
    {

        $id = $this->id;

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

        return $this->applyScopes($restaurant);

    }

return $this->applyScopes($restaurant);$restaurant显示警告:

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

但是,如果我添加->get()类的东西, return $this->applyScopes($restaurant->get());该警告将消失,但datatable视图将得到如下错误:

DataTables warning: table id=dataTableBuilder - ajax error.

http://prntscr.com/el53kh

您可以粘贴您的餐厅课程吗? 我认为您的模型有问题。

无论如何,不​​建议编辑vendor文件夹,因此我决定在v6上也添加流畅的变量。

请更新至v6.27.0以具有此功能。 谢谢!

嗨,这是我目前的餐厅模特班:

<?php

namespace App\Models;

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

/**
 * Class restaurant
 *
 * <strong i="6">@package</strong> App\Models
 * <strong i="7">@version</strong> February 27, 2017, 5:13 pm UTC
 * <strong i="8">@property</strong> int $res_id
 * <strong i="9">@property</strong> string $res_name
 * <strong i="10">@property</strong> string $res_image
 * <strong i="11">@property</strong> string $res_image_date
 * <strong i="12">@property</strong> string $res_address
 * <strong i="13">@property</strong> string $res_state
 * <strong i="14">@property</strong> string $res_location
 * <strong i="15">@property</strong> string $res_area
 * <strong i="16">@property</strong> string $res_telephone
 * <strong i="17">@property</strong> int $res_min_price
 * <strong i="18">@property</strong> int $res_max_price
 * <strong i="19">@property</strong> float $res_latitude
 * <strong i="20">@property</strong> float $res_longitude
 * <strong i="21">@property</strong> bool $res_chain
 * <strong i="22">@property</strong> bool $res_new
 * <strong i="23">@property</strong> bool $res_offer
 * <strong i="24">@property</strong> bool $res_type_state
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\res_working_hour[] $workingHours
 * <strong i="25">@method</strong> static \Illuminate\Database\Query\Builder|\App\Models\restaurant whereResAddress($value)
 * <strong i="26">@method</strong> static \Illuminate\Database\Query\Builder|\App\Models\restaurant whereResArea($value)
 * <strong i="27">@method</strong> static \Illuminate\Database\Query\Builder|\App\Models\restaurant whereResChain($value)
 * <strong i="28">@method</strong> static \Illuminate\Database\Query\Builder|\App\Models\restaurant whereResId($value)
 * <strong i="29">@method</strong> static \Illuminate\Database\Query\Builder|\App\Models\restaurant whereResImage($value)
 * <strong i="30">@method</strong> static \Illuminate\Database\Query\Builder|\App\Models\restaurant whereResImageDate($value)
 * <strong i="31">@method</strong> static \Illuminate\Database\Query\Builder|\App\Models\restaurant whereResLatitude($value)
 * <strong i="32">@method</strong> static \Illuminate\Database\Query\Builder|\App\Models\restaurant whereResLocation($value)
 * <strong i="33">@method</strong> static \Illuminate\Database\Query\Builder|\App\Models\restaurant whereResLongitude($value)
 * <strong i="34">@method</strong> static \Illuminate\Database\Query\Builder|\App\Models\restaurant whereResMaxPrice($value)
 * <strong i="35">@method</strong> static \Illuminate\Database\Query\Builder|\App\Models\restaurant whereResMinPrice($value)
 * <strong i="36">@method</strong> static \Illuminate\Database\Query\Builder|\App\Models\restaurant whereResName($value)
 * <strong i="37">@method</strong> static \Illuminate\Database\Query\Builder|\App\Models\restaurant whereResNew($value)
 * <strong i="38">@method</strong> static \Illuminate\Database\Query\Builder|\App\Models\restaurant whereResOffer($value)
 * <strong i="39">@method</strong> static \Illuminate\Database\Query\Builder|\App\Models\restaurant whereResState($value)
 * <strong i="40">@method</strong> static \Illuminate\Database\Query\Builder|\App\Models\restaurant whereResTelephone($value)
 * <strong i="41">@method</strong> static \Illuminate\Database\Query\Builder|\App\Models\restaurant whereResTypeState($value)
 * <strong i="42">@mixin</strong> \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.
     *
     * <strong i="43">@var</strong> 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
     *
     * <strong i="44">@var</strong> array
     */
    public static $rules = [

    ];


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


}

嘿yajra,这是$ key = $ this-> attributes; 不是$ key = $ this-> key; 对?
我用了这个$ key = $ this-> attributes;

@jcduenasr该类使用一个魔术的getter方法,该方法从属性中的键中获取值。

嗨,我在表单中有两个日期字段,用于按日期过滤表查询,并防止它带来数千条记录。
问题是,当我尝试使用按钮之一进行导出时,查询未发送日期过滤器,因此我什么也没带。
如何使用按钮发送日期字段以进行导出?

这是我的课。
命名空间AppDataTables;
使用App \ Accesos;
使用YajraDatatables \ Services \ DataTable;

AccesosDataTable类扩展了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'));
    }

}

此页面是否有帮助?
0 / 5 - 0 等级

相关问题

kamrava picture kamrava  ·  3评论

Mopster picture Mopster  ·  3评论

nasirkhan picture nasirkhan  ·  3评论

jackrsantana picture jackrsantana  ·  3评论

shadoWalker89 picture shadoWalker89  ·  3评论