title: Lumen - validation rules in model?
body: <p>I am creating a REST API and would like to attach some validation rules to the CREATE and UPDATE endpoints. The validation rules for these two endpoints would be exactly the same, so I would like to be able to specify them in one place only. Coming from a Laravel background I normally create Form Requests, or put the validation rules in the model within a <code>rules()</code> function.</p>
  
  <p>However I think Lumen does validation a bit differently and their documentation suggests to put the validation logic in the router: <a href="https://lumen.laravel.com/docs/master/validation" rel="nofollow noreferrer">https://lumen.laravel.com/docs/master/validation</a></p>
  
  <p>However personally I don't think that is the best place for it and would prefer to put in the model instead. I tried using the <code>rules()</code> function within the model but that doesn't seem to do anything.</p>
  
  <p>My <code>create</code> and <code>update</code> methods look like this:</p>
  
  <pre><code>public function create(Request $request)
  {
      $product = Product::create($request-&gt;all());
  
      return response()-&gt;json($product, 201);
  }
  
  public function update($id, Request $request)
  {
      $product = Product::findOrFail($id);
      $product-&gt;update($request-&gt;all());
  
      return response()-&gt;json($product, 200);
  }
  </code></pre>
  
  <p>Is it possible for me to put my validation rules within my <code>Product</code> model and have them run automatically?</p>
  
  <p>Here is how I have attempted to do it in my model:</p>
  
  <pre><code>namespace App;    
  
  use Illuminate\Database\Eloquent\Model;
  
  class Product extends Model
  {
  
      /**
       * The attributes that are mass assignable.
       *
       * @var array
       */
      protected $fillable = [
          'name', 'price', 'description',
      ];
  
      /**
       * The attributes excluded from the model's JSON form.
       *
       * @var array
       */
      protected $hidden = [];
  
      /**
       * Set model validation rules.
       *
       * @return array
       */
      public function validate()
      {
          return [
              'name' =&gt; 'required',
              'price' =&gt; 'required',
              'description' =&gt; 'required',
          ];
      }
  }
  </code></pre>