21.11.2014 Views

Laravel Starter - PHP User Group (Myanmar)

Laravel Starter - PHP User Group (Myanmar)

Laravel Starter - PHP User Group (Myanmar)

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Laravel</strong> <strong>Starter</strong><br />

Schema::table() accepts two arguments. The first is the name of the table that you'll be<br />

interacting with, in this case it's users. The second argument is a closure which contains your<br />

table definition. The closure receives the argument $table and this is the object that we'll be<br />

interacting with to define the table.<br />

$table->create();<br />

This line tells <strong>Laravel</strong> that the table will need to be created. If we omit this line, Schema will<br />

generate the ALTER TABLE syntax rather than the CREATE TABLE syntax.<br />

$table->increments('id');<br />

The increments() method tells Schema that the specified field should be an autoincremented<br />

primary key. With <strong>Laravel</strong>, you'll want to use simple field names such as id, email,<br />

and password. If you aren't familiar with using Object-Relational Mapping (ORM), you may be<br />

in the habit of creating those same field names with the table name as a prefix. For example,<br />

user_id, user_email, user_password. The purpose behind defining field names with<br />

the table name as a prefix is to simplify query generation when using a query builder. This is no<br />

longer necessary and it's best to follow the more simple convention as it manages redundant<br />

interactions for you, removing the need for you to continuously write the boilerplate code.<br />

$table->string('email');<br />

$table->string('real_name');<br />

$table->string('password');<br />

Next we have a few string declarations. These will be created as the VARCHAR fields with the<br />

default length of 200. You can override the length of these fields by passing a second argument<br />

that represents the intended length. For example:<br />

$table->string('email', 300);<br />

This line creates a VARCHAR field named email with a length of 300.<br />

It's important to note that we shouldn't reduce the size of the password<br />

field as we'll need that length for the output from <strong>Laravel</strong>'s Hash class.<br />

10<br />

$table->timestamps();<br />

Finally, we come to the timestamps() method. This will create two DATETIME fields<br />

(created_at and updated_at). It is not unreasonable to create the timestamp fields for<br />

every table in the database as they can be very useful for troubleshooting down the road. The<br />

Eloquent ORM will automatically manage these timestamp fields for us. So, we can forget<br />

about them for now.<br />

The down() method should revert any changes made to the up() method. In this case, the up()<br />

method creates a database table called users. So, the down() method should remove the table.<br />

Schema::drop('users');

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!