كيف يمكنني تمرير المعلمات (user_id ، اسم المستخدم ، إلخ) إلى فئة 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) {}
؟
إذا كنت تستخدم الإصدار 7 ، فيمكنك استخدام:
class UsersController extends Controller
{
public function index(UsersDataTable $dataTable)
{
return $dataTable->with('key', 'value')->render('users');
}
}
والوصول إليها مثل خاصية في فئة dataTable.
public function query() {
$key = $this->key;
...
}
هذا هو الالتزام حيث أضفت هذه الوظيفة في حال احتجت إلى ذلك. لم يضاف بعد في الوثائق.
مرحبًا ، شكرًا على الرد ، أنا حاليًا أستخدم 6.x لـ Laravel 5.3.30 ،
هل ستنجح إذا قمت بنسخ ولصق رمز الالتزام في جدول البيانات 6.x؟
لقد أضفت كود الالتزام إلى حزمة Datatables 6.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()
like، return $this->applyScopes($restaurant->get());
فسيختفي التحذير ، لكن عرض جدول البيانات سيظهر خطأ مثل:
DataTables warning: table id=dataTableBuilder - ajax error.
هل يمكنك لصق فئة مطعمك؟ أعتقد أن هناك خطأ ما في نموذجك.
على أي حال ، لا يُنصح بتحرير مجلد البائع لذلك قررت إضافة المتغيرات بطلاقة في الإصدار 6 أيضًا.
يرجى التحديث إلى 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؛ حق؟
لقد استخدمت هذا المفتاح $ = $ this-> سمات ؛
jcduenasr ، تستخدم الفئة طريقة getter سحرية تجلب القيم من المفاتيح في السمات.
مرحبًا ، لدي حقلا تاريخ في نموذج لتصفية استعلام الجدول حسب التواريخ ومنعه من إحضار آلاف السجلات لي.
تكمن المشكلة في أنه عندما أحاول التصدير باستخدام أحد الأزرار ، فإن الاستعلام لا يرسل عوامل تصفية التاريخ ، لذلك لا أحضر أي شيء.
كيف أرسل حقول التاريخ باستخدام الأزرار للتصدير؟
هذا صفي.
مساحة الاسم AppDataTables ؛
استخدام App \ Accesos ؛
استخدام YajraDatables \ 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'));
}
}
تمت إضافة المستندات https://yajrabox.com/docs/laravel-datatables/master/buttons-with. شكر!
التعليق الأكثر فائدة
إذا كنت تستخدم الإصدار 7 ، فيمكنك استخدام:
والوصول إليها مثل خاصية في فئة dataTable.