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?
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.
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? 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.
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:
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.
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 ().
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');
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.
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).
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.
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 …
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With