我正在尝试在数据库中显示一个用户的信息。当我将用户id的静态值放入where语句时,查询工作正常:
where('radcheck.username', '=', '5345855');
但是当我把它变成动态时它不起作用并且找不到错误$ id
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
?
答案 0 :(得分:3)
需要使用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);
})
答案 1 :(得分:1)
使用以下可能有效的代码
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]);
答案 2 :(得分:0)
您没有在路线中定义任何动态变量:
Route::resource('users','radcheckController');
要访问控制器中的变量,您必须先在路径中定义它们:
Route::resource('users/{id}','radcheckController');
然后您可以访问控制器中的$id
变量。
答案 3 :(得分:0)
您可以使用use关键字将必要的变量从父作用域传递到闭包中。
在你的情况下改变这一行
$result = DB::table('radcheck')-> leftJoin('radreply', function
($join) {
到这个
$result = DB::table('radcheck')-> leftJoin('radreply', function
($join) use ($id) {
答案 4 :(得分:0)
请检查路线 并使用这条路线 并且不需要任何令牌来获取网址
Route :: get('users / {id}','radcheckController @ show');
答案 5 :(得分:0)
您必须使用use关键字将变量从父作用域传递到闭包中。在您的情况下,您忘记传递$ id
示例:
$result = DB::table('radcheck')-> leftJoin('radreply', function
($join) use ($id) {
$join->on('radcheck.username', '=', 'radreply.username')
->where('radcheck.username', '=', $id);
})