Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
Running raw sql query in migration laravel

How to run raw SQL query in migration Laravel ?

Aman Jain, February 12, 2022February 15, 2022

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('users'); \\or whatever you want on rollback
    }
}

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
Screenshot 2022 02 12 at 12.28.54 PM

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

List all migrations

In your terminal type

php artisan QueryMigrate list
Screenshot 2022 02 12 at 12.26.04 PM
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

Related

Php Laravel laravelmigrationpackage

Post navigation

Previous post
Next post

Related Posts

Php Laravel orderBy clause in eloquent with example

Laravel orderBy clause in eloquent with example

October 5, 2022March 16, 2024

In this blog post, we will take a look at how to use the Laravel orderBy clause in the Eloquent ORM.Laravel provides an expressive, fluent interface for creating and retrieving records in your database. When we work with databases in Laravel, we often need to order the results we get…

Read More
Php How to Get Only Soft Deleted Records in Laravel 9

How to Get Only Soft Deleted Records in Laravel 9 ?

June 20, 2022June 20, 2022

In this article we will learn to get only soft deleted records in Laravel. In our recent article Use Soft Delete to Temporary (Trash) Delete the Records in Laravel 9 we learnt to delete the file without actually deleting from database and sometimes we want to show only that records…

Read More
Php Laravel encrypt and decrypt strings

How to encrypt and decrypt string in laravel 8 ?

January 26, 2022January 26, 2022

Laravel provides inbuilt library to encrypt and decrypt the strings. Laravel usage OpenSSL to provide AES-256 encryption which creates long encrypted string. Encryption and decryption is a technique to hide the useful some information in a form which is not readable. In this tutorial i will show you to encrypt…

Read More

Aman Jain
Aman Jain

With years of hands-on experience in the realm of web and mobile development, they have honed their skills in various technologies, including Laravel, PHP CodeIgniter, mobile app development, web app development, Flutter, React, JavaScript, Angular, Devops and so much more. Their proficiency extends to building robust REST APIs, AWS Code scaling, and optimization, ensuring that your applications run seamlessly on the cloud.

Categories

  • Angular
  • CSS
  • Dart
  • Devops
  • Flutter
  • HTML
  • Javascript
  • jQuery
  • Laravel
  • Laravel 10
  • Laravel 11
  • Laravel 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • May 2025
  • April 2025
  • October 2024
  • July 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • July 2023
  • March 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Recent Posts

  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
  • Understanding High Vulnerabilities in Software: A Week of Insights
  • Blocking Spam Requests with LaraGuard IP: A Comprehensive Guide
  • Enhancing API Development with Laravel API Kit
  • Exploring the Future of Web Development: Insights from Milana Cap
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version