Laravel display validation error



PHP Snippet 1:

    $request->validate([
        'name' => 'required'
    ]);

PHP Snippet 2:

   @if ($errors->has('name'))
      <li>{{ $errors->first('name') }}</li>
   @endif

PHP Snippet 3:

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

PHP Snippet 4:

@if($errors->any())
    <div class="alert alert-danger">
        <p><strong>Opps Something went wrong</strong></p>
        <ul>
        @foreach ($errors->all() as $error)
            <li>{{ $error }}</li>
        @endforeach
        </ul>
    </div>
@endif

@if(session('success'))
    <div class="alert alert-success">{{session('success')}}</div>
@endif

@if(session('error'))
    <div class="alert alert-danger">{{session('error')}}</div>
@endif

PHP Snippet 5:

public function update(Request $request, $id)
    {
        $this->validate($request,[
            'title'=>'required',
            'body'=>'required'
        ]);
       //the above validation is important to get the errors caught 
        $post= Post::find($id);
        $post->title = $request->input('title');
        $post->body = $request->input('body');

        $post->save();

        return redirect('/posts')->with('success','Updated successfully');
    }

PHP Snippet 6:

...
<div class="container">
    @include('message')
    @yield('content')
</div>
...

PHP Snippet 7:

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