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.

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.

Read Also:  How to send mail from localhost XAMPP using PHP?

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:

<?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.

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

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 :

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.