How to Check Table is Exists or Not in Laravel?

12-Dec-2022

.

Admin

How to Check Table is Exists or Not in Laravel?

Hello Friends,

For whatsoever reason, you may want to check the existence of a table in your database. You can easily achieve this by using one of Laravel's schema builder method hasTable(). The Laravel schema builder hasTable() method accepts one argument, the name of the table to check.

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