Video
Description
Automatically generate Laravel validation rules based on your database table schema!
Let's say you've migrated this fictional table:
Schema::create('persons', function (Blueprint $table) { $table->id(); $table->string('first_name', 100); $table->string('last_name', 100); $table->string('email'); $table->foreignId('address_id')->constrained(); $table->text('bio')->nullable(); $table->enum('gender', ['m', 'f', 'd']); $table->date('birth'); $table->year('graduated'); $table->float('body_size'); $table->unsignedTinyInteger('children_count')->nullable(); $table->integer('account_balance'); $table->unsignedInteger('net_income'); $table->boolean('send_newsletter')->nullable();});
Generate rules for a whole table
Now if you run:
php artisan schema:generate-rules persons
You'll get:
Schema-based validation rules for table "persons" have been generated!Copy & paste these to your controller validation or form request or where ever your validation takes place:[ 'first_name' => ['required', 'string', 'min:1', 'max:100'], 'last_name' => ['required', 'string', 'min:1', 'max:100'], 'email' => ['required', 'string', 'min:1', 'max:255'], 'address_id' => ['required', 'exists:addresses,id'], 'bio' => ['nullable', 'string', 'min:1'], 'gender' => ['required', 'string', 'in:m,f,d'], 'birth' => ['required', 'date'], 'graduated' => ['required', 'integer', 'min:1901', 'max:2155'], 'body_size' => ['required', 'numeric'], 'children_count' => ['nullable', 'integer', 'min:0', 'max:255'], 'account_balance' => ['required', 'integer', 'min:-2147483648', 'max:2147483647'], 'net_income' => ['required', 'integer', 'min:0', 'max:4294967295'], 'send_newsletter' => ['nullable', 'boolean']]
As you may have noticed the float-column body_size, just gets generated to ['required', 'numeric'].
Proper rules for float, decimal and double, are not yet implemented!
Related Content on Laravel Daily
Video
Recent Courses on Laravel Daily
Testing in Laravel 13 For Beginners
26 lessons
1 h 41 min read
Laravel 13 Eloquent: Expert Level
41 lessons
1 h 34 min
How to Build Laravel 13 API From Scratch
30 lessons
1 h 23 min