Get the fillable array from a Laravel model

July 5th, 2024 • 1 minute read time

If, for any reason, you need to get the fillable fields from a Laravel model, it's pretty straightforward.

Here's the first way you're able to achieve this:

$model = new User();
dd($model->getFillable());

This returns an array of all the fillable columns.

But we can do better. You can inline it like this:

dd((new User)->getFillable());

And from PHP 8.4, you won't need to parenthesize the instantiation of the model, so you'll be able to do this:

dd(new User()->getFillable());

Perhaps a neater solution is resolving the model out of the container first. Here's what that looks like:

dd(app(Model::class)->getFillable());

So, there are a bunch of ways to get the fillable columns you've defined in your models — all these examples do the same thing. Just choose which fits your style best.

If you found this article helpful, you'll love our practical screencasts.
Author
Alex Garrett-Smith
Share :

Comments

No comments, yet. Be the first!