Skip to main content

Generate Laravel migrations and seeders from an existing database

In conclusion, migrating existing PHP applications to laravel can be a challenging task, perticularly when dealing with large databases and tables.

As a developer we need a solution which automates the migration and seeder generation.Thankfully, there are handy packages available to automate the migration and seeder generation processes.

For generating migrations, you can utilize the `kitloong/laravel-migrations-generator` package.

First we will generate migrations from database tables

Run below command
    composer require --dev kitloong/laravel-migrations-generator
Running below command will generate migrations for all tables
        php artisan migrate:generate

If you want to create migrations for specific tables then use below command
         php artisan migrate:generate --tables="table1,table2,table3"

You can also ignore some tables using
        php artisan migrate:generate --ignore="table3,table4,table5"

Moving on to generating seeders from tables, you can employe the `orangehill/iseed` package. Install it using following command
        composer require orangehill/iseed

If you are using Laravel 5.3.7 or below or Laravel 4 then you need to specify iseed version while composer require like this
      composer require orangehill/iseed:2.2 # Laravel 5.3.7 and below
   
    composer require orangehill/iseed:1.1 # Laravel 4

Latest laravel versions set service providers automatically but if you use Laravel 5.4 or below you need to add service provider in providers array in file app/config/app.php
   // ...
Orangehill\Iseed\IseedServiceProvider::class,
Now run below command using specific table names for generating seeders for each table
php artisan iseed table1,table2,table3
You can use  --force to force an existing seeder to re generate

php artisan iseed users --force
By automating the migration and seeder generating processes, these packages save developers significant time and effort. Give them a try and streamline your Laravel application development experience. Happy coding!

Comments

Popular posts from this blog

Build laravel crud using infyom laravel generator

Installation of Laravel First you need to install laravel by running below composer commands     composer create-project laravel/laravel example-app After creating project run artisan command to initialize development server     cd example-app     php artisan serve Now you can access your application in your web browser at http://localhost:8000 Installing Infyom laravel generator Now you need to install laravel generator by infyom. Add following packages into composer.json while using it with Laravel 9.     "require": {                  "infyomlabs/laravel-generator" : "^5.0",                  "infyomlabs/adminlte-templates" : "^5.0"      } if you want to use Generate from table option, you need to install       "require": {                  "doctrine/dbal" : "^2.3"     ...

Introduction Of Laravel

Laravel is a PHP based web framework for building high-end web applications using its significant and graceful syntaxes. It comes with a robust collection of tools and provides application architecture. Moreover, it includes various characteristics of technologies like ASP.NET MVC, CodeIgniter, Ruby on Rails, and many more. This framework is an open-source framework. It facilitates developers by saving huge time and helps reduce thinking and planning to develop the entire website from scratch. Along with that, the security of the application is also taken care of by Laravel. So all its features can increase the speed of web development for you. If you are familiar with intermediate PHP scripting and PHP basics, Laravel can prepare your work more efficiently. The current version of Laravel is  9 , which is a stable release, released on  February 8, 2022 . And anyone can check its open-source GitHub repository from this link below: https://github.com/laravel/framework Laravel Fa...