Validate Request in Controller

Fri May 14 2021 22:59:51 GMT+0000 (UTC)

Saved by @slendabilt

* Controller Code

// type-hint the Request parameter so we can simply reference $request
  public function store(Request $request)
    $this->validate($request, [
      'name' => 'required|unique:categories'

* View Code

// I can then display error alerts in the view
@if ($errors->any())
      <ul class="list-group">
          @foreach ($errors->all() as $error)
          <div class="alert alert-danger" role="alert">
              {{ $error }}

This code allows me to validate the request to specify rules. I can then loop through the errors and display them in the view.