How to Check Table is Exists or Not in Laravel?
Dec 12, 2022
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...
- How to Notifications With database Driver in Laravel 11?
- How to Send Email Via Notification in Laravel 11?
- How to Install and Configure Laravel 11 Debugbar?
- How to Like Dislike System in Laravel 11?
- How to Paginate a Model with Relationship in Laravel 11?
- Laravel 11 Upload Files to Amazon S3 Example
- Laravel 11 Send WhatsApp Messages Example
- Laravel 11 Rest API Authentication Using JWT Tutorial