Search code examples
phpmysqllaravellaravel-5laravel-5.5

Laravel create table with raw string


How can I create a new table in laravel 5.5 with a raw DB string?

$createTableSqlString =
  "CREATE TABLE $newTableName (
      product_id INT(11) NOT NULL AUTO_INCREMENT,
      ...
    )
    COLLATE='utf8_general_ci'
    ENGINE=InnoDB
    AUTO_INCREMENT=1;";

I know there is a Illuminate\Support\Facades\Schema for making a new table,

Schema::connection('mysqlInvsys')->create(...);

But how should I define or give it the raw string DB::raw($createTableSqlString) ? I did not find any example in the official doc.


Solution

  • Laravel provides functions for running raw SQL queries as shown here.

    For table creation, you can use DB::statement($createTableSqlString)