140

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 ? Commented Jun 10, 2017 at 1:17

7 Answers 7

315

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');
Sign up to request clarification or add additional context in comments.

8 Comments

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.
try DB::connection('xxxx')->getSchemaBuilder()->hasTable('xxx')
i try this and it's working... DB::getSchemaBuilder()->hasTable('table_name_without_prefix')
Schema::connection("bio_db")->hasTable('deviceLogs_11_2019')
@AbelCallejo it is Illuminate\Support\Facades\Schema
|
34

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.

Comments

12

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

Comments

6

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.

Comments

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 Comments

Thanks!.. I'm using Laravel 3.
This is not fully cross-compatible across database types. For instance, it doesn't work with Sqlite or Oracle.
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.

Comments

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

Still working on Laravel 10

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.