HasModelSchema Trait Upgrade: Removing getFillable for Better Validation in Laravel #47952
Replies: 2 comments
-
Is this already implemented? I don't recall seeing an PR for this. If not... I am not a big fan of its name. Modelschema sounds more like it's defining the schema of the table, rather than specifying validation... Also, it should not replace fillables and guards. Rather complement them. As there are enough use cases where this would block things and you may have fields that you did not specify as you don't need them which might get sent to you anyway. |
Beta Was this translation helpful? Give feedback.
-
@henzeb thanks for the input, this is a work in progress locally. You are more than welcome to suggest something that you think might be the best suited for this proposal. Another name that I think might work is <?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Attributes\FieldsDecorator;
#[FieldsDecorator([
'name' => 'required|string|max:255',
'email' => 'required|email|unique:test_users,email',
'age' => 'integer|min:18',
'gender' => 'required|in:male,female,other',
'is_active' => 'boolean',
])]
class User extends Model
{
public $table = 'test_users';
public $timestamps = false;
protected $guarded = [];
} and you could do something like: $user = new User;
$user->name = "Taylor";
$user->is_active = true;
$user->validate();
// Or
$user->validate(fn($rules) => []); // to remove all rules
if($user->isValid()){
// perform other actions
} I think this might also complement other attributes like |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Laravel, being a popular PHP framework, continues to evolve with powerful new features. In this update, I am thrilled to introduce the
ModelSchema
attribute, designed to simplify and streamline data validation for models.Imagine a scenario where you have a model named
User
orProduct
, and you want to define specific database fields and their validation rules without using the traditionalfillable
orguarded
properties. TheModelSchema
attribute is the perfect solution for this.Let me illustrate this feature using the example of a
User
model:In this example, the
ModelSchema
attribute lets you conveniently define the attributes and their validation rules directly in the model, using a clean and expressive syntax. The attributes are listed along with their respective validation constraints, such asrequired
,string
,email
,integer
,min
,in
, andboolean
.This approach simplifies the validation process and ensures that only valid data is stored in the database. Gone are the days of manually defining the
fillable
orguarded
properties and dealing with complex validation logic in your controllers.Now, validating incoming data becomes seamless using the model's schema. For instance, in a controller, you can utilize the
validate
method to automatically validate incoming requests against the defined schema rules:With the
ModelSchema
attribute, you achieve cleaner code, maintain a separation of concerns, and experience a more streamlined development process. Embrace this feature to enjoy the benefits of simplified data validation in your Laravel models!Beta Was this translation helpful? Give feedback.
All reactions