Eloquent models from my database

Cristian Llanos
Oct 17, 2016 · 3 min read
Making artisan code my Eloquent models

Eloquent models are a very convenient way of representing our database entities in Laravel projects. I believe many would agree with this view. However, I think there should be an even quicker way to map my tables into Eloquent models.

The thing is, our databases have all the information needed to scaffold our Eloquent models. So, why don’t we take advantage of that? I mean, the table knows that this field should be an integer, the other one should be a date, and that one should be a “boolean” and so on.

I’ve used Yii2 in some of my projects and I really like its code generation capabilities. It’s astonishingly powerful, trust me, with just some little tweaks, Gii can make your life way easier. I’ve looked for some packages for Laravel with similar features, but I didn’t find anything like that.

So I decided I could help a little and start with Eloquent model generation. Eloquent has so many beautiful things for example its casting system and the way it handles dates converting them into carbon instances.

I spent some weekends trying to make it work and I finally have it. It’s now an artisan command and I’m planning to give it a GUI (I think it’ll be nice).

Hey, folks! Let me introduce you to php artisan code:models command. You can try it out by importing my package through composer with:

composer require reliese/laravel

After that, you will need to register the service provider:

Reliese/Coders/CodersServiceProvider::class

And publish the configuration file:

php artisan vendor:publish --tag=reliese-models

Now you are ready to use it. Just make sure you have configured your database and you can safely issue the command:

php artisan code:models

There’s still a lot of room for improvement, but for now this command will take care of:

  • Casts: It’ll add casts for your attributes based on their database types.
  • Dates: All database date or datetime fields will be added to this property.
  • PHP type hints for each model: This will help IDEs to auto-complete the model fields.
  • One-to-one relationships.
  • One-to-many relationships.
  • Many-to-many relationships using belongsToMany.
  • Primary key: It is smart enough to know when to add $incrementing = false.
  • Timestamps: If your table does not have the timestamps fields, it will set $timestamps = false.
  • Soft Deletes: If your table has soft deletes attributes, it will add the SoftDeletes trait.
  • You can tell it whether your fields are not snake cased and it will respect that.
  • Table name: If your table name is not plural or if it doesn’t comply with the convention, it will fill the $table property.
  • Fillable attributes: It will give you a good start with fillable attributes so you don’t have to write them. It won’t put primary keys or passwords within this property.
  • Hidden attributes: You can tell it which pattern of fields should be hidden. There are some like “*password”, “*token” and “*secret*” by default, but you can change it.

At the time of writing, support is available for Laravel 5.1 or above and MySQL databases.

You can find my project onGitHub. That’s it. I hope you find it useful :)

Cristian Llanos

Written by

Lead Software Developer at Fandango Latam

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade