Php 如何在不嵌入Ajax的情况下获取Laravel URL参数的值
我使用的是Laravel5.6、PHP7.1和XAMPP。我有一个URL,它包含来自一个控制器的值,并且以前定义过。 我的问题是,如何获取LaravelURL中的最后一个值而不在之前传递值。我下面的代码也会显示这个问题 这是我的Ajax:Php 如何在不嵌入Ajax的情况下获取Laravel URL参数的值,php,ajax,laravel,laravel-5,Php,Ajax,Laravel,Laravel 5,我使用的是Laravel5.6、PHP7.1和XAMPP。我有一个URL,它包含来自一个控制器的值,并且以前定义过。 我的问题是,如何获取LaravelURL中的最后一个值而不在之前传递值。我下面的代码也会显示这个问题 这是我的Ajax: $(document).ready(function() { $('#student_table').DataTable({ "processing": true, "serverSide": true,
$(document).ready(function() {
$('#student_table').DataTable({
"processing": true,
"serverSide": true,
"ajax": "{{ route('leads.getdata') }}",
"columns":[
{ "data": "group_id" },
{ "data": "customer_id" },
{ "data": "customer_id" },
{ "data": "action", orderable:false, searchable: false},
{ "data":"checkbox", orderable:false, searchable:false}
]
});
我的路线:
Route::get('leads/getdata', 'Controller@getdata')->name('leads.getdata');
Controller.php
function getdata(Request $request)
{
//$id = $request->input('id');
$students = GroupCustomer::select('id', 'name', 'address')->where('user_id', '=', $id);
return Datatables::of($students)
->addColumn('action', function($student){
return '<a href="/customers/'.$student->id.'" class="btn btn-xs btn-primary eye" id="'.$student->id.'"><i class="glyphicon glyphicon-eye-open"></i></a><a href="#" class="btn btn-xs btn-danger delete" id="'.$student->id.'"><i class="glyphicon glyphicon-remove"></i></a>';
})
->addColumn('checkbox', '<input type="checkbox" name="student_checkbox[]" class="student_checkbox" value="{{$id}}" />')
->rawColumns(['checkbox','action'])
->make(true);
}
函数getdata(请求$Request)
{
//$id=$request->input('id');
$students=GroupCustomer::选择('id','name','address')->其中('user_id','=','id');
返回数据表::of($students)
->addColumn('action',函数($student){
返回“”;
})
->addColumn('复选框','')
->rawColumns(['checkbox','action']))
->使(真实);
}
当前URL为:http://localhost:8000/leads/6
因为我需要获取“6”值并将其传递给查询以获取该用户的姓名、地址。目前,它在没有“where”语句的情况下工作。但是,我只想显示用户_id='6'的名称和地址
有没有一种方法可以在不改变Ajax和路由的情况下获得这个“6”值 将路线更改为:
Route::get('leads/{id}', 'Controller@getdata')->name('leads.getdata');
将控制器更改为:
function getdata(Request $request, $id) {
//$id = $request->input('id');
$students = GroupCustomer::select('id', 'name', 'address')->where('user_id', '=', $id)->get();
return Datatables::of($students)
->addColumn('action', function($student){
return '<a href="/customers/'.$student->id.'" class="btn btn-xs btn-primary eye" id="'.$student->id.'"><i class="glyphicon glyphicon-eye-open"></i></a><a href="#" class="btn btn-xs btn-danger delete" id="'.$student->id.'"><i class="glyphicon glyphicon-remove"></i></a>';
})
->addColumn('checkbox', '<input type="checkbox" name="student_checkbox[]" class="student_checkbox" value="{{$id}}" />')
->rawColumns(['checkbox','action'])
->make(true);
}
函数getdata(请求$Request,$id){
//$id=$request->input('id');
$students=GroupCustomer::select('id'、'name'、'address')->where('user_id'、'='、$id)->get();
返回数据表::of($students)
->addColumn('action',函数($student){
返回“”;
})
->addColumn('复选框','')
->rawColumns(['checkbox','action']))
->使(真实);
}
我认为不接触ajax或route是不可能实现您想要的功能的。问题是通过绑定数据解决的,实际上问题是绑定数据。多姆。没有很好地工作,所以一旦我解决了绑定的问题,它就工作得很好 这是我修复DOM问题后的Ajax代码:
var id = document.getElementById("customer_id").value;
$('#student_table').DataTable({
"processing": true,
"serverSide": true,
ajax: {
url: "{!! route('leads.getdata') !!}",
type: "GET",
data: {id, id},
dataType: "JSON"
},
"columns":[
{ "data": "group_id" },
{ "data": "customer_id" },
{ "data": "customer_id" },
{ "data": "action", orderable:false, searchable: false},
{ "data":"checkbox", orderable:false, searchable:false}
]
});
谢谢我得到了这行:{“draw”:0,“recordsTotal”:0,“recordsFiltered”:0,“data”:[…],“querys”:[{“query”:“select count(*)as aggregate from(select'1'as
row\u count
fromgroup\u customers
其中customer\u id
=?)count\u row\u table”,“bindings:[“1”,“time”:0.48},{“query”:“选择id
,name
,address
fromgroup\u customers
whereuser\u id
=?”,“bindings”:[“6”],“time”:0.32},“input”:[]}如您所见,“where”条件为空。如何获取和传递“bindings”的值:[“6”]您必须添加->get(),比如:$students=GroupCustomer::select('id','name','address')->where('user_id','=','id','id)->get();您还可以缩短它的使用时间,比如:$students=GroupCustomer::where('user_id','id)->get(['id','name','address'));我不明白你的问题,但我想你是在问什么?你也可以使用,这样你就可以在控制器中无需任何查询就获得整个GroupCustomer
模型。检查那些文档,它们是否有帮助?如果没有,请尝试澄清你的问题,我不确定你在问什么。是的,没错。我试图在到pas的过程中绑定数据让它们转到下一页,但它不断向我显示一个错误,请告诉我如何在这个Ajax脚本中绑定数据。