Make your Laravel project modular pt. 2

We already have our modules in our project, let’s find out how to create model, migration, seeder, controller and using routes also views in our modules. Basically nwidart/laravel-modules already has commands to create everything that we need. For example to create a model in Blog modules we use this command :

php artisan module:make-model <model-name> <module_name>
# Example
php artisan module:make-model Blog Blog

The model file will be generated on Entities folder of your modules.

Let’s make migration to create blog table. Use php artisan module:make-migration <migration_name> <module_name> command, for example :

php artisan module:make-migration create_blogs_table Blog

Then you can create your own schema from blogs table. Mine is like this :

Then you can migrate your migration using command below :

# Migrate migration
php artisan module:migrate <module_name>
# Rolling back migration
php artisan module:migrate-rollback <module_name>

Make sure you have already setting up your database configuration in .env . To filled up our blogs table, let’s create a seeder using following command :

php artisan module:make-seed <seeder_name> <module_name>
# For Example
php artisan module:make-seed Blog Blog

Let’s say we create our seeder like this or you can create your own as you like.

Then you can call your BlogTableSeeder in BlogDatabaseSeeder :

Then run your seeder with php artisan module:seed Blog command, and now you can check your blogs table in your database or you can use php artisan tinker like picture below :

By default after generating a new module, it already has a controller inside the module. Or we can create our controller using following command :

php artisan module:make-controller <controller_name> <module_name>
# For Example
php artisan module:make-controller ThingController Blog

Now in the controller you can do what do you want, in this I will show all blogs table records into blog::index views in my BlogController.

Then we need to add some modify to our routes and views.

By default the routes has already provides the index route, you modify or create new route as you want.

Then the final result will be something like this.

That’s all that I can share about Laravel module and thanks for read my post, if you want to discuss or have any question please feel free to leave a comment. Share with others if you found this post useful. See ya



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store