Hy, i have question about creating table(generally working with DB) in laravel through migrations.
I have something like this(from code happy)
<?php
Schema::create('users', function($table) {
$table->increments('id');
$table->string('username', 32);
$table->string('email', 320);
$table->string('password', 64);
$table->integer('role');
$table->boolean('active');
$table->timestamps();
});
OK this will create table "users" with 9 fields, but I'm getting confused by this callback. First of all variable "$table" is instance of which class? Can someone explain me what happened here, respectively how this works?
Well, here's how it happens:
Schema::create()
, it creates a Blueprint
object, linked to the table's name you passed as argument.Closure
that will receive this previously created Blueprint
object as the $table
argument, and act on it. $table
object, it is actually wiring it to a Grammar
class, according to your database. That is, if you are using a MySQL database, it will use MySqlGrammar
class. This ensures you get valid SQL for any database you use, without having to worry about it.If you wish to see the actual SQL queries generated, you can add the --pretend
option to the migrate
command. I'd recommend saving it in a file, so you can read it more easily. Ex:
php artisan migrate --pretend > app/storage/migration.sql
This would save it in app/storage/migration.sql
file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With