Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to detect if a database table exists with Laravel

Tags:

php

laravel

I want to be able to create a table using

Schema::create('mytable',function($table)
{
    $table->increments('id');
    $table->string('title');
});

But before that I would like to check if the table already exists, perhaps something like

Schema::exists('mytable');

However, the above function does not exist. What else can I use?

like image 512
Ehsan Zargar Ershadi Avatar asked Apr 10 '13 20:04

Ehsan Zargar Ershadi


People also ask

How will you check table is exists or in the database?

To check if table exists in a database you need to use a Select statement on the information schema TABLES or you can use the metadata function OBJECT_ID(). The INFORMATION_SCHEMA. TABLES returns one row for each table in the current database.

How do you check if table exists in a migration?

Solution to this is Schema::hasTable() method. So, if table flights does not exist, then we create it. Of course, it can be used in a positive way – for example, if we want to add a new column to existing table, then we may check if table does exist.

How to check table is exists or not in Laravel?

How to check table is exists or not in Laravel? Laravel provide database migrations using Schema facade. Schema facade have sevaral method like add column, remove column, check if table exists, check if column exists etc. In bellow example you can see how to detect if a database table exists with Schema Builder.

How to check if a query returns any records in Laravel?

Or you could use this awesome script to do the installation: Laravel provides a really nice method called exists, which allows you to check if your query returns any records rather than using the count method. I always prefer to use the exists method, but here is an alternative example using count:

How can I make sure my Laravel application doesn't break?

To make sure your Laravel application doesn't break when you are applying changes to your database, it's a good practice to check whether a table exists or not before doing any calls.

Is Laravel count() cross-compatible with other database types?

I'm using Laravel 3. This is not fully cross-compatible across database types. For instance, it doesn't work with Sqlite or Oracle. Rather, depend on information schema query instead of checking for some data in the tables with COUNT ().


6 Answers

If you are using Laravel 4 or 5 then there is the hasTable() method, you can find it in the L4 source code or the L5 docs:

Schema::hasTable('mytable');
like image 113
Phill Sparks Avatar answered Sep 24 '22 13:09

Phill Sparks


To create a new table there is only one check by Laravel Schema function hasTable.

if (!Schema::hasTable('table_name')) {
    // Code to create table
}

But if you want to drop any table before checking its existence then Schema have a function called dropIfExists.

Schema::dropIfExists('table_name');

It will drop the table if table will exist.

like image 38
Brn.Rajoriya Avatar answered Sep 21 '22 13:09

Brn.Rajoriya


As Phill Sparks answered, you can check whether a table exists using:

Schema::hasTable('mytable')

Notice that there are cases your app uses different connections. In this case, you should use:

Schema::connection('myConnection')->hasTable('mytable')

(Don't forget to use use Schema; on the beginning of your code).

like image 26
guyaloni Avatar answered Sep 25 '22 13:09

guyaloni


if you are using different connection then you have to go with my answer.

Schema::connection("bio_db")->hasTable('deviceLogs_11_2019')

here on hasTable() function you can pass more than 1 table name.

like image 26
pankaj kumar Avatar answered Sep 24 '22 13:09

pankaj kumar


No built in function for this in L3. You can do a raw query:

$table = "foo";
$check = DB::only('SELECT COUNT(*) as `exists`
    FROM information_schema.tables
    WHERE table_name IN (?)
    AND table_schema = database()',$table);
if(!$check) // No table found, safe to create it.
{
    // Schema::create …
}
like image 36
mckendricks Avatar answered Sep 21 '22 13:09

mckendricks


Rather, depend on information schema query instead of checking for some data in the tables with COUNT().

SELECT table_schema 
FROM information_schema.tables
WHERE table_schema = DATABASE()
      AND table_name = 'table_name';

Change your 'table_name' value.

If you get one row output, it means the table exists.

like image 40
Bimal Poudel Avatar answered Sep 24 '22 13:09

Bimal Poudel