How to Write Raw SQL Query Migration in Laravel

Written by readerstacks | Published 2022/02/18
Tech Story Tags: migration | write-sql-query | laravel | sql | sql-beginner-tips | writing-sql-queries | sql-database | software-engineering

TLDRWe have created a laravel package to keep the track of raw SQL migration using package [Laravel raw sql query migration] The migration file is used to keep track of the tables created by Laravel. We use the package to create tables and modify the columns in the database but we also wanted to share the tables across the developers. We need to update your application configuration in order to register the package so it can be loaded. The package is then published to publish a table in database named as 'Query Migration'via the TL;DR App

Sometimes we use phpMyAdmin or any other tool to create our tables and to modify the columns in the database but we also wanted to keep track of them to share the schema across the developers.

So for this purpose, we have created a laravel package to keep the track of raw SQL migration using the package Laravel raw sql query migration

You can read the documentation here https://github.com/Readerstacks/laravel_query_migration

Let’s understand how it works

Install the Package

To install the package we can simply run the below command

composer require readerstacks/querymigration

Above Command will update your composer.json

Add provider to config.php

You need to update your application configuration in order to register the package so it can be loaded by Laravel, just update your config/app.php file adding the following code at the end of your 'providers' section:

<?php

return [
    // ...
    'providers' => [
        Readerstacks\QueryMigration\QueryMigrationServiceProvider::class,
        // ...
    ],
    // ...
];

Publish config file

Now publish config file so we can keep track of our raw SQL migrations

php artisan vendor:publish --provider="Readerstacks\QueryMigration\QueryMigrationServiceProvider"

Now create table in database for raw query using below command

php artisan migrate

Above Command will create a table in database named as query_migrations .

Basic usage SQL Migration package

Run Migrations

php artisan QueryMigrate migrate

Laravel Usage

Add Query

php artisan QueryMigrate add --run

This will ask to enter the query to update the migration file and also run the query in the database

If you want to update the migration and not wanted to run in-database then remove –run option as below

php artisan QueryMigrate add 

Check pending migrations

In your terminal type

php artisan QueryMigrate pending

Run migrations

In your terminal type

php artisan QueryMigrate migrate

List all migrations

In your terminal type

php artisan QueryMigrate list

list all migration list


Run single migration only

In your terminal type

php artisan QueryMigrate migrate --uid=uid_of_migration 

Run single migration again

In your terminal type

php artisan QueryMigrate migrate --uid=uid_of_migration  --f

To get the UID of migration you can open config/query_migration_config.php and find UID of query.


Remove single migration only

In your terminal type

php artisan QueryMigrate remove --uid=uid_of_migration 


Remove All migration

In your terminal type

php artisan QueryMigrate removeAll


Remove single migration only

In your terminal type

php artisan QueryMigrate remove --uid=uid_of_migration 


Check pending migration

In your terminal type

php artisan QueryMigrate pending


Also published here.


Written by readerstacks | Our Readerstacks blogs are the most useful for software, PHP, Angular, React, Flutter and Laravel to improve skils.
Published by HackerNoon on 2022/02/18