Ad

Schema::create Vs Sql

I am new with Laravel and started my personal project.

Laravel provide migrations for creating schema for our databases. I have difficulties on implementation choice. The ideas I have are:

  1. Create database schema using Illuminate\Support\Facades\Facade\Schema provided by Laravel, for example:
Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('username',300)->unique();
            $table->string('email',300)->unique();
            $table->string('password',100);
})
  1. Create schema using ERD Designer like Mysql Workbench, export the diagram to sql and finally execute the sql in Laravel using DB::statement

I'm trying to google which method is better, but no answer satisfy me.

Which methods is the better or commonly used in industry? If you have any other suggestion I'm happy to know it.

Ad

Answer

Laravel Schema is an abstraction and, for the most, database indipendent, that is, if there is already a grammar in the laravel source or in an external library you can use the same migrations with different Database Engines with minor issues.

If you use Raw SQL you could, in case, depend on the actual Database Engine you use.

Laravel ships with these grammars: MySQL, PostgreSQL, SQLite and SQLServer.

If you use the build-in Schema you can use the same migrations in any of the databe engines above without worry too much about the actual SQL implementation.

Consider that many developers have a local dev environment with a simple database engine like SQLite, and sometimes when they start a project they don't even know the database engine that will be used in production. Think about the case you want to change hosting provider or you have a demo project on a cheap provider and the production elsewhere.

Ad
source: stackoverflow.com
Ad