Raw sql query migration using package

How to write raw sql query migration in laravel ?

Laravel provides its inbuilt feature to take care of migrations, Migrations like to make the version control for our database schema and share it across the developers but in laravel migrations we need to create eloquent query Using the schema class or Facade and then we need to run it using php artisan migrate command.

If you wanted to use laravel migration then we can use as below in our migration file

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateTestTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
DB::statement('ALTER TABLE `users` MODIFY `age` DATETIME');
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('test');
}
}

Also Read : How to run raw query laravel eloquent ?

Raw SQL migration using package

Sometimes we use phpMyAdmin or any other tool to create our tables and to modify the columns in 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 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 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 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

Read Full documentation here : https://readerstacks.com/how-to-run-raw-sql-query-in-migration-laravel/

--

--

--

Our Readerstacks blogs are the most useful for software, PHP, Angular, React, Flutter, Devops and Laravel to improve their skills? Read our updated list.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

CS373 Spring 2020: Paul Sathuluri

Compose Generator — Update 1.3.0

Operating Systems for beginners — MMU

Virtual Machines vs Containers

Inbol.net http://ift.tt/2kGYna7

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
Readerstacks

Readerstacks

Our Readerstacks blogs are the most useful for software, PHP, Angular, React, Flutter, Devops and Laravel to improve their skills? Read our updated list.

More from Medium

Laravel 8: how to manage Many-to-Many relationships with sync() method

Configuring Laravel 7 For Use With PhpRedis using Homestead

Laravel vs. NodeJS: Compare the most Demanding Back-End Web Framework

MongoDB Beginner To Advanced With Practical Code Snippet Example