Reputation: 9381
As per title: How can I specify that when trying to save my model (pushing it to the database) that certain columns cannot be null?
e.g. protected $fillable = ['name', 'haircolor'];
I know that I can use a [Mutator][1]
but that doesn't work when creating a new object with form data as input, only for individual assingment.
I might use the before save event but that also sounds kinda hacky.
So is there a way to define the constraints in an eloquent model? Or maybe if not, a best practice?
Upvotes: 2
Views: 649
Reputation: 163748
Best practice is to define the column as not nullable in migration class by removing ->nullable()
.
You also could create a wrapper for create()
function, like:
public function checkAndCreate($data) {
// Check if some columns are null and do something about this.
....
return $this->create($data);
}
Upvotes: 2