laravelsessionmessagebag

how to display message bag object in laravel blade template?


I want to display my errors in Laravel blade template. How should i go about it?

return view('edit', [
    "id"      => $request->id,
    "data"    => $data,
    "errors"  => $validator->messages(),
    "success" => null,
]);

Expected output is

The mahajan first name may only contain letters.

But This is displayed if i print $errors.

{
  "mahajan_first_name":["The mahajan first name may only contain letters."],
  "mahajan_middle_name":["The mahajan middle name may only contain letters."],
  "mahajan_last_name":["The mahajan last name may only contain letters."]
}

Solution

  • You can print them in an unordered list like this:

    @foreach ($errors->all() as $error)
       <li>{{ $error }}</li>
    @endforeach
    

    A single error is shown like this:

    {{ $errors->first('mahajan_first_name') }}
    

    To check if an error exists you can do:

    @if($errors->has('mahajan_first_name'))
     // your code here
    @endif