125

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?

1
  • Could you tell to what file you added this code ? Jun 10, 2017 at 1:17

7 Answers 7

298

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');
8
  • call_user_func_array() expects parameter 1 to be a valid callback, class 'Illuminate\Database\MySqlConnection' does not have a method 'hasTable', I used DB::hasTable('test')because Schema class not found. Mar 22, 2017 at 6:44
  • 12
    try DB::connection('xxxx')->getSchemaBuilder()->hasTable('xxx')
    – efinal
    Jan 23, 2018 at 1:58
  • 1
    i try this and it's working... DB::getSchemaBuilder()->hasTable('table_name_without_prefix') Jun 30, 2019 at 4:56
  • Schema::connection("bio_db")->hasTable('deviceLogs_11_2019')
    – pankaj
    Nov 26, 2019 at 19:33
  • 2
    @AbelCallejo it is Illuminate\Support\Facades\Schema May 28 at 19:33
33

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.

10

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).

5

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.

4

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 …
}
2
  • 1
    Thanks!.. I'm using Laravel 3. Apr 12, 2013 at 17:04
  • This is not fully cross-compatible across database types. For instance, it doesn't work with Sqlite or Oracle.
    – Benubird
    Jan 26, 2015 at 14:12
1

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.

0
if (!Schema::hasTable('table_name')) {
 enter code here
}

Still working on Laravel 10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.