在下面的代码中,是否可以在if语句之外返回响应?

时间:2016-11-17 04:50:44

标签: laravel

在下面的代码中,是否可以在if语句之外返回响应?

if($staff->attendence == 0)
{
$response = ['message' => 'The Staff is absent']
return response($response,422)
}

2 个答案:

答案 0 :(得分:1)

试试这个:

if($staff->attendence == 0)
{
    //If stuff absent return response should be like this.
    $response = ['message' => 'The Staff is absent'];
    return response()->json($response, 422);
}

//Other wise if staff is present 
return response()->json(['message' => 'The staff is present.'], 200);

答案 1 :(得分:0)

喜欢这个??

if($staff->attendence == 0) {
  $response = ['message' => 'The Staff is absent']
  $flag = true;
}

if($flag) {
    return response($response,422);
}