我正在创建API我的控制器代码:
public function isUserGroup(Request $request)
{
$id = $request->input('id');
$isGroup = $request->input('isGroup');
if($isGroup == false)
{
$users = DB::table('users')
->join('user_basic_info','users.id','=','user_basic_info.user_id')
->select('users.id','user_basic_info.first_name as name')
->where('users.id',$id)->get();
$resultArray['users'] = $users;
}
if ($isGroup == true)
{
$groups = DB::table('group') // Only this is working
->select('group.id','group.name')
->where('group.id',$id)->get();
$resultArray['group'] = $groups;
return \Illuminate\Support\Facades\Response::json($resultArray, 200);
}
}
现在这里只返回组详细信息,或者我将条件isGroup设置为false或true,仅返回组详细信息 我不知道我哪里错了。
我们将非常感谢您的帮助!
答案 0 :(得分:0)
像这样更改行:
$isGroup = $request->input('isGroup') ? true : false;
答案 1 :(得分:0)
首先将isGroup
更改为:
$isGroup = $request->input('isGroup') ? true : false;
然后将==
更改为===
,您的代码还有其他问题。应该是这样的:
if($isGroup === false)
{
$users = DB::table('users')
->join('user_basic_info','users.id','=','user_basic_info.user_id')
->select('users.id','user_basic_info.first_name as name')
->where('users.id',$id)->get();
$resultArray['users'] = $users;
}else if ($isGroup === true)
{
$groups = DB::table('group') // Only this is working
->select('group.id','group.name')
->where('group.id',$id)->get();
$resultArray['group'] = $groups;
}
return \Illuminate\Support\Facades\Response::json($resultArray, 200);
如果仍然无法正常工作,则可能是您输入的isGroup
是错误的,如果输入为字符串'false'将返回true。因此,如果要返回用户,则应清空isGroup输入。
如果您输入的是字符串,则如下所示:
$isGroup = $request->input('isGroup');
if($isGroup == 'false')
{
$users = DB::table('users')
->join('user_basic_info','users.id','=','user_basic_info.user_id')
->select('users.id','user_basic_info.first_name as name')
->where('users.id',$id)->get();
$resultArray['users'] = $users;
}else if ($isGroup == 'true')
{
$groups = DB::table('group') // Only this is working
->select('group.id','group.name')
->where('group.id',$id)->get();
$resultArray['group'] = $groups;
}
return \Illuminate\Support\Facades\Response::json($resultArray, 200);
答案 2 :(得分:0)
您需要检查数据类型。
尝试:
$isGroup = $request->input('isGroup');
var_dump($isGroup); // this will tell you type of data.
一旦确定类型,就可以继续进行。