Laravel-datatables: DataTableサービスUsersDataTableクラスにパラメータを送信する方法は?

作成日 2017年03月15日  ·  11コメント  ·  ソース: yajra/laravel-datatables

DBクエリのDataTable Service UsersDataTableクラスにパラメータ(user_id、usernameなど)を渡すにはどうすればよいですか?

UsersController:

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

public function query($param) {}public function ajax($param) {}ようにUsersDataTableクラスの関数をカスタマイズする必要がありますか?

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

これは、必要に応じてこの機能を追加したコミットです。 ドキュメントにはまだ追加されていません。

こんにちは、返信ありがとうございます。現在、Laravel5.3.30に6.xを使用しています。

コミットコードをコピーしてdatatables6.xに貼り付けても機能しますか?

コミットコードをDatatables6.xパッケージに追加しましたが、DataTableサービスクラス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());の警告は消えますが、データテーブルビューには次のようなエラーが表示されます。

DataTables warning: table id=dataTableBuilder - ajax error.

http://prntscr.com/el53kh

レストランのクラスを貼り付けてもらえますか? モデルに問題があると思います。

とにかく、ベンダーフォルダーを編集することはお勧めできないので、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');
    }


}

やあヤジュラ、それは$ key = $ this-> attributesです。 $ key = $ this-> keyではありません; 正しい?
私はこの$ key = $ this-> attributes;を使用しました。

@jcduenasrクラスは、属性のキーから値をフェッチするマジックゲッターメソッドを使用します。

こんにちは。フォームに2つの日付フィールドがあり、テーブルクエリを日付でフィルタリングして、何千ものレコードが表示されないようにしています。
問題は、ボタンの1つを使用してエクスポートしようとすると、クエリが日付フィルターを送信しないため、何も表示されないことです。
ボタンを使用して日付フィールドを送信してエクスポートするにはどうすればよいですか?

これは私のクラスです。
名前空間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 評価