SoFunction
Updated on 2025-04-07

Laravel method to verify error message to blade template

Background code:

 /**
   * POST
   * admin/cate
   * Add to
   */
 public function store(Request $request)
 {
  $input = $request->all();
 
  $rule = [
   'name' => 'required',
  ];
 
  $message = [
   '' => 'name not allow null'
  ];
 
 
  $validate = Validator::make($input, $rule, $message);
 
  if (!$validate->passes()) {
   return back()->withErrors($validate);
 
  }
  return redirect('admin/cate');
 }

Use: $validate->errors();

You can see the error object returned by the validator. It is troublesome to retrieve the error message string, so use the withError() function to return the entire $validate to the foreground page.

If you want to add an error message, you can

 $validate->errors()->add('key','error message');
    return back()->withErrors($validate);

Front-end page code:

 @if(count($errors)>0)
    @foreach($errors->all() as $value
     {{$value}}
    @endforeach
 @endif

In this way, the front desk can receive the error message.

The above method of verifying error information to the blade template of laravel is all the content I share with you. I hope you can give you a reference and I hope you can support me more.