Laravel:使用自定义错误重定向但没有错误消息

Consider the manual authentication. If the order ID has not been found in database, we redirect user to page with input fields and with error 'wrongOrderId':

public function login(Request $request) {

    $inputted_orderId = $request->input('order_id');       
    $orderIdInDB = DB::table(self::SITE_IN_DEVELOPMENT_TABLE_NAME)
            ->where(self::ORDER_ID_FIELD_NAME, $inputted_orderId)->first();

   if (is_null($orderIdInDB)) {
       return Redirect::route('loginToSitesInDevelopmentZonePage')->withErrors('wrongOrderId');
   }               
}

In this example, we don't need to pass the error message: the message box is already exists in View; all we need is to display this message box when user has been redirected with error 'wrongOrderId':

@if (!empty($errors->first('wrongOrderId'))) 
    <div class="signInForm-errorMessagebox" id="invalidID-errorMessage">
        <!-- ... -->
    </div>
@endif

All above code is working without laravel/php errors; I get into is_null($orderIdInDB) if-block when input wrong order number, however the error message box don't appears. I tried a lot of ways, and (!empty($errors->first('wrongOrderId'))) is just last one. What I doing wrong?

Did you try printing {{$errors->first()}}?

first(string), works as a key value pair, it invokes its first key VALUE

try this,

 @if($errors->any())
    <div class="signInForm-errorMessagebox" id="invalidID-errorMessage">
        <!-- ... -->
    </div>
 @endif

If you want to get specific error from validator error, use the get() method.

$errors->get('wrongOrderId'); get all the wrongOrderId errors.

$errors->first('wrongOrderId'); get first error of wrongOrderId errors

I explored that $errors->first('wrongOrderId') has non-displayable but non-null value. So @if ( $errors->first('wrongOrderId') !== null ) will work.

Something more elegantly like @if ($errors->first('wrongOrderId'))? Unfortunately just one this check is not enough: even if we define

return Redirect::route('loginToSitesInDevelopmentZonePage')->withErrors(['wrongOrderId' => true]);

php will convert true to 1. So, @if ( $errors->first('wrongOrderId') == true ) will working, but @if ($errors->first('wrongOrderId')) will not.

However, we can to cast $errors->first('wrongOrderId') to bool, so the most elegant solution will be

@if ((bool) $errors->first('wrongOrderId'))