How to create custom module in Laravel 9
In this article, we will learn about how we can create custom module in Laravel 9.
To create custom module in Laravel 9 we will use nwidart/laravel-modules.
nwidart/laravel-modules is a Laravel package that was created to manage your large Laravel app using modules. The module is like a Laravel package, it has some views, controllers or models. This package is supported and tested in Laravel 5+
The nwidart/laravel-modules package is a re-published, re-organised and maintained version of ‘pingpong/modules’, which isn’t been maintained anymore. For more information about this package. Click Here.
What we all learn in this article
- Installation of Laravel Application
- Installation of nwidart/laravel-modules packages via Laravel
- How to create a module
- How to create a model
- How to create a migration
- How to create a controller
- How to create a seeder
- How to delete a module
Step – 1 Installation of Laravel Application
Run the below command to install Laravel
composer create-project --prefer-dist laravel/laravel learn
Step -2 Now we will install our nwidart/laravel-modules package
Run the below command to install nwidart/laravel-modules package.
composer require nwidart/laravel-modules
Step -3 How to create custom module
Run the below command to create a module under your nwidart/larave-modules
php artisan module:make Customer
The above will command will create Customer module, you can check under Module/customer.
Step – 4 How to create a model
Run the below command to create a model
php artisan module:make-model Customer Customers
Step – 5 How to create a migration and model together
Run the below command to create a migration and model together, here -m is use for creating the model, model will created inside your entities folder.
php artisan module:make-model Customer -m Customers
Step – 6 How to create a controller
Run the below command to create a controller inside your module
php artisan module:make-controller CustomersController Customer
Read Also: One to one relationship in Laravel 8
Step – 7 How to create a seeder
Run the below command to create a seeder
php artisan module:make-seed StatesTableSeeder Customers
The above code will generate a seed file like the one shown below:
<?php namespace Modules\Customers\Database\Seeders; use Illuminate\Database\Seeder; use Illuminate\Database\Eloquent\Model; class StatesDatabaseSeeder extends Seeder { /** * Run the database seeds. * * @return void */ public function run() { Model::unguard(); // $this->call("OthersTableSeeder"); } }
Step-: 8 How to Delete a module
php artisan module:delete Customers
I hope you like the above article, and you understood how to create a custom module in laravel 8, if you like the article please rate us 5