Improved model generation with Laracademy Generators


September 2nd, 2016


Laravel provides the Artisan command line tool that allows you to save time by including several generators. Some examples include make:controller, make:model, and make:migration.

Building on top of this idea is a third party package named Laracademy Generators that will automatically generate your models based on your database structure.


Let’s explore the workflow when using Laracademy Generators:

First, create your migration as always:

1php artisan make:migration create_posts_table —create=posts

Then add your fields to the generated migration file. For example, your field might look like:

1public function up()
3 Schema::create(‘posts’, function (Blueprint $table) {
4 $table->increments(‘id’);
5 $table->string(‘title’);
6 $table->text(‘body’);
7 $table->boolean(‘featured’);
8 $table->datetime(‘publish_date’);
9 $table->timestamps();
10 });

After that, you will migrate your tables by execution the following command in your terminal:

1php artisan migrate
2Migrated: 2016_08_26_145636_create_posts_table

Now let’s install Laracademy Generators:

1composer require "laracademy/generators"

Then add Laracademy Generators into your config/app.php file:


Alternatively, you can add the provider to your ‘app/Providers/AppServiceProvider.php’ if you only want to use this provider for local development:

1public function register()
3 if($this->app->environment() == 'local') {
4 $this->app->register('\Laracademy\Generators\GeneratorsServiceProvider');
5 }

Now you have everything you need to start using the Laracademy Generators. Let’s explore how to use it and what are the available options.If you check your Artisan cli, a new command will appear in the list:


First, you can pass –all flag to tell Laracademy Generators to generate models for all tables existed in your database:

1php artisan generate:modelfromtable --all

Another option is –table which makes you be able to specify the tables that you want Laracademy Generators to generate models for, like this:

1php artisan generate:modelfromtable --table=posts

Or multiple tables at once:

1php artisan generate:modelfromtable --table=users,posts

There are another two options; one is for choosing your database connection (–connection=example) and the second is for specifying you where the generated models goes (–folder=app\Models).

Take a look at what the generated model looks like, if you are following along with me in the posts table example, the generated app/Post.php content will look like this:


Just note that the generated code will often require some edits but it gives you a great starting point.

Give Laracademy Generators a try if you are looking for away to speed up your development process. You can check out the source code of Laracademy Generators at Github.

Filed in:

Diaa Fares

Laravel Artisan, Web Developer, Tech Geek