目前,我在显示页面上,我正在运行我的控制器中的show
函数,所以我的url在url地址栏中显示为dashboard/1/people
。现在,当我点击一个人时,它会路由到另一个页面,这就是调用getPeople
的地方。
我如何从1
中的ajax请求获取我点击的人的id,这是scripts
并传递给我的控制器?
PS:目前,我在ajax请求中有硬编码的1
,但我希望它是动态的请
我怎么做到这一点?
脚本
datatable = $('#table').DataTable({
"ajax": "{{ route('dashboard/1/people') }}",
"columns": [
{data: 'check', name: 'check'},
],
调节器
public function show($id)
{
$class = Class::whereId($id)->first();
return view('show');
}
public function getPeople($id)
{
$get_id = $id;
$class = Class::whereId($get_id)->first();
$people = $class->peoples()->get();
return Datatables::of($people)->addColumn('action', function ($ppl) {
//return
})->make(true);
}
这应该工作:
在你的getPeople
方法中,将id存储在session
变量中:
public function getPeople($id)
{
$get_id = $id;
//using session helper method
session(['show_id' => $id]);
$class = Class::whereId($get_id)->first();
$people = $class->peoples()->get();
return Datatables::of($people)->addColumn('action', function ($ppl) {
//return
})->make(true);
}
然后在你的ajax代码中访问它:
datatable = $('#table').DataTable({
"ajax": "{{ route('dashboard/'.session('show_id').'/people') }}",
"columns": [
{data: 'check', name: 'check'},
],
DataTable ajax允许您以对象格式传递额外的参数,如下所示:
datatable = $('#table').DataTable({
"ajax": {
"type": "GET",
data:{id: my_id_var},
"url": "my_route"
}
}
在你的函数中只需获取Request var
public function getPeople(Request $request){
$get_id = $request->id;
$class = Class::whereId($get_id)->first();
$people = $class->peoples()->get();
return Datatables::of($people)->addColumn('action', function ($ppl) {
//return
})->make(true);
}
更多信息在Sorce Page