Laravel : 使用取自 url 的参数作为动态 table 名称
Laravel : use parameter that taken from url for dynamic table name
在控制器中,我想使用取自 url 的动态 table 名称,然后使用包 chumper/datatable.
显示数据
当我浏览 mysite.com/unit/rentin/send 时出现 AJAX 错误,因为使用的 table 名称是 send_ {unit} 而我注册的 table 名字是 send_rentin.
这是我的代码:
routes.php
Route::resource('unit.send', 'SendController');
SendController.php
class SendController extends \BaseController {
public function index($unit)
{
$table = new Send; // Send is my model
$table->setTable('send_' .$unit);
if (Datatable::shouldHandle())
{
return Datatable::collection($table::all())
->showColumns('admin_no')
->addColumn('admin_date', function ($model) {
return date('d M Y', strtotime($model->admin_date));
})
->make();
}
return View::make('send.index')->withUnit($unit);
}
}
}
我错过了什么?
经过几个小时的尝试,我在 views 中发现了一个错误。
错误
{{ Datatable::table()
->addColumn('no', 'date')
->setUrl(route('unit.send.index'))
->render('datatable.uikit') }}
右
{{ Datatable::table()
->addColumn('no', 'date')
->setUrl(route('unit.send.index', ['unit'=>$unit]))
->render('datatable.uikit') }}
在控制器中,我想使用取自 url 的动态 table 名称,然后使用包 chumper/datatable.
显示数据当我浏览 mysite.com/unit/rentin/send 时出现 AJAX 错误,因为使用的 table 名称是 send_ {unit} 而我注册的 table 名字是 send_rentin.
这是我的代码:
routes.php
Route::resource('unit.send', 'SendController');
SendController.php
class SendController extends \BaseController {
public function index($unit)
{
$table = new Send; // Send is my model
$table->setTable('send_' .$unit);
if (Datatable::shouldHandle())
{
return Datatable::collection($table::all())
->showColumns('admin_no')
->addColumn('admin_date', function ($model) {
return date('d M Y', strtotime($model->admin_date));
})
->make();
}
return View::make('send.index')->withUnit($unit);
}
}
}
我错过了什么?
经过几个小时的尝试,我在 views 中发现了一个错误。
错误
{{ Datatable::table()
->addColumn('no', 'date')
->setUrl(route('unit.send.index'))
->render('datatable.uikit') }}
右
{{ Datatable::table()
->addColumn('no', 'date')
->setUrl(route('unit.send.index', ['unit'=>$unit]))
->render('datatable.uikit') }}