Using models for data

Although most of the time we want to store data inside of our database and use our models to represent that data in an object-oriented way, there might be times when you want an Eloquent model specifically for data.

Laravel1 / 3Level:

Although there are plenty of ways to store data outside of the database, such as Data Transfer Objects (DTO's), JSON files or config files, there have been a few situations where I still wanted the benefits of using an Eloquent model, like so:

use App\Models\Field;

class ActiveField extends Field {
     * The model's attributes.
     * @var array
    protected $attributes = [
        'name' => 'active',
        'label' => 'Active',
        'control' => 'toggle',
        'value' => true,
        'width' => 6,

This example was taken directly from my own use case, by the way. In my case, I used a Field class, which simply extends Laravel's Illuminate\Database\Eloquent\Model class. While there are also rows in my database for all kinds of fields, there were times where I simply wanted to use a default field that I could customize locally through code.

Now whenever I need this class, I simply refer to its class and it will be filled automatically. While some may argue this is inconsistent with storing data in the database, I actually found this to be a convenient approach.

🎁 Bonus

I have to confess, I actually lied to you about not storing anything in the database. In my particular case I added an additional column to my fields table called "model_type", which refers to this class name whenever I need it. This technique, also known as "morphing", is perfect for these type of scenarios. You don't need to store anything in the database, but simply point to the class that contains this data.

I hope you found this little tip useful. Maybe you won't need to use it straight away, but perhaps knowing it's out there to use could prove just as valuable.

Thanks for reading and hope to see you around!