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:
- 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);
})
- 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.
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.
Related Questions
- → "failed to open stream" error when executing "migrate:make"
- → October CMS Plugin Routes.php not registering
- → OctoberCMS Migrate Table
- → OctoberCMS Rain User plugin not working or redirecting
- → October CMS Custom Mail Layout
- → October CMS - How to correctly route
- → October CMS - Conditionally Load a Different Page
- → Make a Laravel collection into angular array (octobercms)
- → In OctoberCMS how do you find the hint path?
- → How to register middlewares in OctoberCMS plugin?
- → Validating fileupload(image Dimensions) in Backend Octobercms
- → OctoberCMS Fileupload completely destroys my backend
- → How do I call the value from another backed page form and use it on a component in OctoberCms