Php 在laravel控制器中找不到$id
我试图在数据库中显示一个用户的信息。当我将用户id的静态值放入where语句时,查询工作正常:Php 在laravel控制器中找不到$id,php,laravel,laravel-5,laravel-query-builder,Php,Laravel,Laravel 5,Laravel Query Builder,我试图在数据库中显示一个用户的信息。当我将用户id的静态值放入where语句时,查询工作正常: where('radcheck.username', '=', '5345855'); 但是,当我将其转换为动态时,它不工作,并且出现错误$id not found where('radcheck.username', '=', $id); 我不知道为什么我看不到$id,因为如果我写echo$id,它会被成功打印出来。这是控制器的代码: public function show($id) {
where('radcheck.username', '=', '5345855');
但是,当我将其转换为动态时,它不工作,并且出现错误$id not found
where('radcheck.username', '=', $id);
我不知道为什么我看不到$id
,因为如果我写echo$id
,它会被成功打印出来。这是控制器的代码:
public function show($id)
{
echo $id;
$result = DB::table('radcheck')
-> leftJoin('radreply', function ($join) {
$join->on('radcheck.username', '=', 'radreply.username')
->where('radcheck.username', '=',$id);
})
->select(
'radcheck.username',
DB::raw("max(case when radcheck.attribute = 'Cleartext-Password' then radcheck.value end) as password"),
DB::raw("max(case when radcheck.attribute = 'Expiration' then radcheck.value end) as expiration"),
DB::raw("max(case when radcheck.attribute = 'ChilliSpot-Max-Total-Octets' then radcheck.value end) as quta"),
DB::raw("max(case when radcheck.attribute = 'Simultaneous-Use' then radcheck.value end) as simul"),
DB::raw("max(case when radcheck.attribute = 'Max-All-Session' then radcheck.value end) as session"),
DB::raw("max(case when radreply.attribute = 'WISPr-Bandwidth-Max-Up' then radreply.value end) as upload"),
DB::raw("max(case when radreply.attribute = 'WISPr-Bandwidth-Max-Down' then radreply.value end) as download"),
DB::raw("max(radcheck.adsoyad) as realname"),
DB::raw("min(radcheck.dtarih) as birthdate"),
DB::raw("min(radcheck.telefon) as phone"),
DB::raw("min(radcheck.tcno) as tc"),
DB::raw("max(radcheck.email) as email"),
DB::raw("min(radcheck.id) as id")
)
->get();
return response()->json(['data' => $result]);
}
这是我的路线定义:
Route::resource('users', 'radcheckController');
我使用ajax发送数据,这是我的ajax代码:
$('.edit-user').on('click', function() {
var id=$(this).data('id');
$.ajax({
type: 'GET',
url: '/users/' + id,
data: {
'_token': $('input[name=_token]').val(),
},
success: function(hr_request) {
alert(hr_request);
var obj=hr_request.data['0'];
alert(obj['username']);
$('#username').val(obj['username']);
$('#password').val(obj['password']);
$('#realname').val(obj['realname']);
$('#mobile').val(obj['phone']);
$('#email').val(obj['email']);
$('#birthdate').val(obj['birthdate']);
$('#simul').val(obj['simul']);
$('#tc').val(obj['tc']);$('#email').val(obj['email']);
$('#date_expir').val(obj['expiration']);
$('#quta').val(obj['quta']/1024/1024);
$('#sessiontime').val(obj['session']/60);
$('#edit-modal-label').html('Edit Selected Row');
$('#addModal').modal('show')
},
error: function(hr_request) {
console.log(hr_request['username']) ;
}
});
});
因此,我的问题是,为什么查询不能识别
$id
?使用以下可能有效的代码
public function show($id)
{
echo $id;
$result = DB::table('radcheck')-> leftJoin('radreply', function
($join) use($id) {
$join->on('radcheck.username', '=', 'radreply.username')
->where('radcheck.username', '=',$id);
}) ->select('radcheck.username',
DB::raw("max(case when radcheck.attribute = 'Cleartext-Password' then
radcheck.value end) as password"),
DB::raw("max(case when radcheck.attribute = 'Expiration' then radcheck.value end) as expiration"),
DB::raw("max(case when radcheck.attribute = 'ChilliSpot-Max-Total-Octets' then radcheck.value end) as quta"),
DB::raw("max(case when radcheck.attribute = 'Simultaneous-Use' then radcheck.value end) as simul"),
DB::raw("max(case when radcheck.attribute = 'Max-All-Session' then radcheck.value end) as session"),
DB::raw("max(case when radreply.attribute = 'WISPr-Bandwidth-Max-Up' then radreply.value end) as upload"),
DB::raw("max(case when radreply.attribute = 'WISPr-Bandwidth-Max-Down' then radreply.value end) as download"),
DB::raw("max(radcheck.adsoyad) as realname"),
DB::raw("min(radcheck.dtarih) as birthdate"),
DB::raw("min(radcheck.telefon) as phone"),
DB::raw("min(radcheck.tcno) as tc"),
DB::raw("max(radcheck.email) as email"),
DB::raw("min(radcheck.id) as id")
)
->get();
return response()->json(['data' => $result]);
使用以下可能有效的代码
public function show($id)
{
echo $id;
$result = DB::table('radcheck')-> leftJoin('radreply', function
($join) use($id) {
$join->on('radcheck.username', '=', 'radreply.username')
->where('radcheck.username', '=',$id);
}) ->select('radcheck.username',
DB::raw("max(case when radcheck.attribute = 'Cleartext-Password' then
radcheck.value end) as password"),
DB::raw("max(case when radcheck.attribute = 'Expiration' then radcheck.value end) as expiration"),
DB::raw("max(case when radcheck.attribute = 'ChilliSpot-Max-Total-Octets' then radcheck.value end) as quta"),
DB::raw("max(case when radcheck.attribute = 'Simultaneous-Use' then radcheck.value end) as simul"),
DB::raw("max(case when radcheck.attribute = 'Max-All-Session' then radcheck.value end) as session"),
DB::raw("max(case when radreply.attribute = 'WISPr-Bandwidth-Max-Up' then radreply.value end) as upload"),
DB::raw("max(case when radreply.attribute = 'WISPr-Bandwidth-Max-Down' then radreply.value end) as download"),
DB::raw("max(radcheck.adsoyad) as realname"),
DB::raw("min(radcheck.dtarih) as birthdate"),
DB::raw("min(radcheck.telefon) as phone"),
DB::raw("min(radcheck.tcno) as tc"),
DB::raw("max(radcheck.email) as email"),
DB::raw("min(radcheck.id) as id")
)
->get();
return response()->json(['data' => $result]);
需要使用
use
关键字将变量传递到函数内部,然后只能在函数内部访问该变量
就像这样:
$result = DB::table('radcheck')-> leftJoin('radreply', function
($join) use ($id) { // Notice `use ($id)` here.....
$join->on('radcheck.username', '=', 'radreply.username')
->where('radcheck.username', '=', $id);
})
需要使用
use
关键字将变量传递到函数内部,然后只能在函数内部访问该变量
就像这样:
$result = DB::table('radcheck')-> leftJoin('radreply', function
($join) use ($id) { // Notice `use ($id)` here.....
$join->on('radcheck.username', '=', 'radreply.username')
->where('radcheck.username', '=', $id);
})
您没有在路由中定义任何动态变量:
Route::resource('users','radcheckController');
要访问控制器中的变量,必须首先在路由中定义它们:
Route::resource('users/{id}','radcheckController');
然后您可以访问控制器中的
$id
变量。您没有在路由中定义任何动态变量:
Route::resource('users','radcheckController');
要访问控制器中的变量,必须首先在路由中定义它们:
Route::resource('users/{id}','radcheckController');
然后,您可以访问控制器中的
$id
变量。您可以使用use关键字将必要的变量从父范围传递到闭包中
在你的情况下,改变这一行
$result = DB::table('radcheck')-> leftJoin('radreply', function
($join) {
对此
$result = DB::table('radcheck')-> leftJoin('radreply', function
($join) use ($id) {
您可以使用use关键字将必要的变量从父范围传递到闭包中 在你的情况下,改变这一行
$result = DB::table('radcheck')-> leftJoin('radreply', function
($join) {
对此
$result = DB::table('radcheck')-> leftJoin('radreply', function
($join) use ($id) {
请检查路线
使用这条路线
获取url不需要任何令牌
路由::get('users/{id}','radcheckController@show'); 请检查路线
使用这条路线
获取url不需要任何令牌
路由::get('users/{id}','radcheckController@show'); 必须使用use关键字将变量从父范围传递到闭包中。在您的情况下,您忘记传递$id 例如:
$result = DB::table('radcheck')-> leftJoin('radreply', function
($join) use ($id) {
$join->on('radcheck.username', '=', 'radreply.username')
->where('radcheck.username', '=', $id);
})
必须使用use关键字将父范围中的变量传递到闭包中。在您的情况下,您忘记传递$id 例如:
$result = DB::table('radcheck')-> leftJoin('radreply', function
($join) use ($id) {
$join->on('radcheck.username', '=', 'radreply.username')
->where('radcheck.username', '=', $id);
})
发布您的路线和htmlI编辑了我的问题@RahulShrivastavacheck我的回答发布您的路线和htmlI编辑了我的问题@RahulShrivastavacheck我的回答这就是我试图发布的内容,并看到了您的答案。感谢您的回复,但它不起作用,当我添加console.log(response()->json(['data'=>$result]);console undefined error displayOk那么$id在不传递use函数的情况下是如何工作的呢?答案缺少解释,这实际上比一个工作代码段更有价值。原因是,只有一个解释会在将来帮助其他读者解决类似(但不是完全相同)的问题,而一个工作片段则不会。当我编写url时,它成功地工作了,并返回了{“data”:[{“username”:“32424123”,“password”:null,“expiration”:null}]}但是我无法从ajax代码中检索数据这是我试图发布的内容,并且看到了您的答案。感谢您的回复,但它不起作用,当我添加console.log(response()->json(['data'=>$result]);console undefined error displayOk那么$id在不传递use函数的情况下是如何工作的呢?答案缺少解释,这实际上比一个工作代码段更有价值。原因是,只有一个解释会在将来帮助其他读者解决类似(但不是完全相同)的问题,而一个工作片段则不会。当我编写url时,它成功地工作了,并返回了{“data”:[{“username”:“32424123”,“password”:null,“expiration”:null}}},但我无法从ajax代码中检索数据