Laravel Drop hascolumn if exists in Migration

Today, We want to share with you Laravel Drop hascolumn if exists in Migration.In this post we will show you laravel eloquent check if record exists, hear for laravel check if model has column we will give you demo and example for implement.In this post, we will learn about how to create table through migration in laravel with an example.

Laravel Drop hascolumn if exists in Migration

There are the Following The simple About Laravel Drop hascolumn if exists in Migration Full Information With Example and source code.

Read Also:  laravel Add cascade on delete (ON DELETE CASCADE)

As I will cover this Post with live Working example to develop Laravel Migrations – Dropping columns, so the laravel check if column exists for this example is following below.

Laravel drop column using migration.

And then here We are going to explain how to drop column step by step.

We can display we can use simple hasColumn(), as We can display below:

Schema::hasColumn(my_db_table_name, my_column_name);

And then We can display below full Laravel migration source code with example:

Read Also:  Laravel 6 drop column if exists MySQL using Migration

<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration
{
    public function up()
    {
        Schema::table('products', function (Blueprint $table) {
            $table->increments('id');
            $table->string('product_name');
            $table->string('p_code')->unique();
            $table->string('p_rate');
            $table->rememberToken();
            $table->timestamps();
			
        });
    }
    public function down()
    {
        if (Schema::hasColumn('products', 'product_name'))
        {
            Schema::table('products', function (Blueprint $table)
            {
                $table->dropColumn('product_name');
            });
        }
    }
}

Last step To run the Database migration using command prompt.

php artisan migrate:rollback

Example 2: Laravel – Drop column if exists using migration

How to check column exists or not in table Laravel 5.7

if (Schema::hasColumn('subscriptions', 'stripe_id')) {
    Schema::table('subscriptions', function ($table) {
        $table->dropColumn('stripe_id');
    });
}

Angular 6 CRUD Operations Application Tutorials

Read :

Read Also:  Laravel Create Table using Migration and model

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about Laravel Drop hascolumn if exists in Migration.
I would like to have feedback on my Pakainfo.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.


Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  Laravel 6 change date format example Tutorial
  2. Read Also:  Simple Pagination with Laravel and MySQl
  3. Read Also:  How to show selected value from database in dropdown using Laravel?
  4. Read Also:  Dynemically HTML table from AJAX response using PHP
  5. Read Also:  C#.Net Enum(Enumeration) Tutorial with Examples
  6. Read Also:  Eloquent ORM Soft Delete in Laravel 6
  7. Read Also:  Laravel Drop all tables with MySQL
  8. Read Also:  Check if Object is empty in VueJS
  9. Read Also:  PHP Creating Dynamic Directory (Folder) Example
  10. Read Also:  Pattern programs in GO Example
CLOSEX