在 laravel 控制器中找不到 $id
$id is not found in laravel controller
我正在尝试显示数据库中一位用户的信息。当我将用户 ID 的静态值放入 where 语句时,查询工作正常:
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]);
需要在函数内部使用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
变量。
您可以使用 use 关键字将必要的变量从父作用域传递到闭包中。
在你的情况下改变这一行
$result = DB::table('radcheck')-> leftJoin('radreply', function
($join) {
至此
$result = DB::table('radcheck')-> leftJoin('radreply', function
($join) use ($id) {
请检查路线
并使用这条路线
get url
不需要任何令牌
路线::获取('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);
})
我正在尝试显示数据库中一位用户的信息。当我将用户 ID 的静态值放入 where 语句时,查询工作正常:
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]);
需要在函数内部使用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
变量。
您可以使用 use 关键字将必要的变量从父作用域传递到闭包中。
在你的情况下改变这一行
$result = DB::table('radcheck')-> leftJoin('radreply', function
($join) {
至此
$result = DB::table('radcheck')-> leftJoin('radreply', function
($join) use ($id) {
请检查路线 并使用这条路线 get url
不需要任何令牌路线::获取('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);
})