Laravel Check Table is Exists or Not Example

Dec 12, 2022 . Admin



Hello Friends,

You might need to confirm that a table is there in your database for any number of reasons. Utilizing one of Laravel's schema builder methods, hasTable, will enable you to accomplish this effortlessly. The name of the table to check is the only argument accepted by the Laravel schema builder's hasTable() method.

You can use this example with the versions of laravel 6, laravel 7, laravel 8, and laravel 9.

Install Laravel

This is optional; however, if you have not created the laravel app, then you may go ahead and execute the below command:

composer create-project laravel/laravel example-app

You might want to check out our lesson on creating table fields by clicking here. To extract the table and call the fields, we employ the model class.

App/Http/Controllers/ProductController
<?php
  
namespace App\Http\Controllers;
    
class ProductController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index()
    {        
        if (Schema::hasTable('table_name'))
        {
            dd('Table Exist...');
        }
    }
}
Output
Table Exist...    

I hope it can help you...

#Laravel