Find Out If a Column Exists in Your Laravel Table

 How to check column exist or not in a table in Laravel

Find-Out-If-a-Column-Exists-in-Your-Laravel-Table

In this article we will learn how to check column exist or not in a table Laravel. Laravel provides lots of methods to functionalities to deal with different types of database requirements. Like table you can also check table exist or not in a table and DB exist or not etc. Here is a below example to check column exist or not in a table in your connected database.

HomeController.php :-

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Schema;
use DB;

class HomeController extends Controller
{
    public function checkColumnExist(){
        try{
            $has_table = Schema::hasColumn('table_name','coumn_name');
            return $has_table;
        }
        catch(\Exception $e){
            return false;
        }
    }
}

If your column exist in the table you will get true otherwise false .



Previous Post Next Post

Contact Form