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?

Phill Sparks
  • 20,000
  • 3
  • 33
  • 46
Ehsan Zargar Ershadi
  • 24,115
  • 17
  • 65
  • 95

7 Answers7

297

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');
Phill Sparks
  • 20,000
  • 3
  • 33
  • 46
  • `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. – 151291 Mar 22 '17 at 06:44
  • 12
    try DB::connection('xxxx')->getSchemaBuilder()->hasTable('xxx') – efinal Jan 23 '18 at 01:58
  • 1
    i try this and it's working... `DB::getSchemaBuilder()->hasTable('table_name_without_prefix')` – Syamsoul Azrien Jun 30 '19 at 04:56
  • Schema::connection("bio_db")->hasTable('deviceLogs_11_2019') – pankaj Nov 26 '19 at 19:33
  • @ShakeelAhmed table and view information are stored separately in the schema. You can query the schema yourself as suggested at https://stackoverflow.com/a/57959796/1269513 – Phill Sparks Jun 30 '21 at 12:41
  • @PhillSparks Thanks for your suggestion, But I don't want to use Raw Query because it will not work if Database engine is changed.. I was looking if Eloquent provides some method like hasTable(). but no success. – Shakeel Ahmed Jun 30 '21 at 13:25
  • What is the **namespace** of `Schema` ? – Abel Callejo May 23 '23 at 05:18
  • 1
    @AbelCallejo it is `Illuminate\Support\Facades\Schema` – Phill Sparks May 28 '23 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.

Brn.Rajoriya
  • 1,534
  • 2
  • 23
  • 35
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).

guyaloni
  • 4,972
  • 5
  • 52
  • 92
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.

pankaj
  • 1
  • 17
  • 36
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 …
}
mckendricks
  • 377
  • 1
  • 3
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.

simhumileco
  • 31,877
  • 16
  • 137
  • 115
Bimal Poudel
  • 1,214
  • 2
  • 18
  • 41
0
if (!Schema::hasTable('table_name')) {
 enter code here
}

Still working on Laravel 10

Syamlal
  • 714
  • 1
  • 11
  • 20