Search code examples
phpvalidationlaravel-5laravel-artisan

How do I validate the data for a Laravel 5 model in a artisan command?


I have a command that fetches data from an API and push it to my local database using an artisan command. As it's a command, I don't have a Request.

How do I validate the model data? I need to check if the description has only 10 characters and if the due_at date is greater than today.

Rely on database validation is not an option.


Solution

  • Validation doesn't need a request object. You can pass any array as the first parameter here:

    $validator = Validator::make([
        'title' => 'This is too long',
        'body' => 'Hello world!'
    ], [
        'title' => 'required|max:10',
        'body' => 'required',
    ]);