Home » Laravel Drop hascolumn if exists in Migration

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:  PHP Delete All Directory Files In Directory

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 Add cascade on delete (ON DELETE CASCADE)

<?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 Exception Handling Example Tutorial

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:  Material Design Add icons in Select options
  2. Read Also:  Codeigniter File Download Helper - Download file from database - URL using Server
  3. Read Also:  Simple Vue.js Form Validation Example
  4. Read Also:  Convert Javascript Arrays Comma separated values
  5. Read Also:  AngularJS Datepicker Example Code With Demo
  6. Read Also:  Eloquent ORM Soft Delete in Laravel 6
  7. Read Also:  Laravel Create Table using Migration and model
  8. Read Also:  PayPal Standard Payment Gateway Integration in Vuejs
  9. Read Also:  OneDrive Move copy create delete file folder
  10. Read Also:  Laravel 6 drop column if exists MySQL using Migration

Leave a Comment

Your email address will not be published. Required fields are marked *