Laravel Classroom image

osdyui
 
To post your Question Join Classroom
 
Lesson Topics's No:-  First|1 | 2|
Lessons:-Migrations

9.2 Migrations
Let’s get started by creating a migration file.
We are going to use Laravel’s command line interface artisan to create our new migration. To
run Artisan you will need to have PHP CLI installed. Normally this will be installed along side
your web server. You will also need to open a terminal to the root of your Laravel package where
the ‘artisan’ file exists. Right, let’s type our first artisan command :
1 php artisan migrate:make create_users
We are telling artisan to run the make method on the migrate task. We pass a name for our
migration to identify it by, I like to name it after the action being performed. In this case we are
creating the users table.
Let’s have a look at the result.
1 Great! New migration created!
Enthusiastic! If we take a look in our application/migrations directory, we will see a new
file named 2012_03_30_220459_create_users.php. Well yours might not be called that! You
see artisan takes the current date, and adds the time in His format to create the file name. The
reason for this is that dates are very important to migrations (and single people). The system
needs to be able to know in which order to apply the changes.
Let’s open up the file and take a look at the migration class.
1 <?php
2
3 class Create_Users {
4
5 /**
6 * Make changes to the database.
7 *
8 * @return void
9 */
10 public function up()
11 {
12 //
13 }
14
15 /**
16 * Revert the changes to the database.
17 *
18 * @return void
19 */20 public function down()
21 {
22 //
23 }
24
25 }
As you can see, our migration class consists of two methods. up() is responsible for making all
of your database changes, where down() accomplishes the exact reverse. This way a migration
can be performed, and rolled back when necessary. If you were to create a table within the up()
method, we would DROP the same table in the down() method.
So how do we perform changes on our database, no doubt with some complex ugly SQL query
right? Haha, no. We are using Laravel, if it’s ugly, it’s not in the framework. Let’s have a look
at creating a table with the Laravel Schema class.
1 <?php
2
3 Schema::create('users', function($table) {
4 // auto incremental id (PK)
5 $table->increments('id');
6
7 // varchar 32
8 $table->string('username', 32);
9 $table->string('email', 255);
10 $table->string('password', 64);
11
12 // int
13 $table->integer('role');
14
15 // boolean
16 $table->boolean('active');
17
18 // created_at | updated_at DATETIME
19 $table->timestamps();
20 });
We call the create() method on the Schema class to create a new table, and pass the name of
the table to be created, and a closure as parameters. We also need to pass a parameter to the
closure. You can call it whatever you like, but I like using $table because I think it makes the
code read better.
Inside the closure, we can use the $table parameter to create our fields with a number of handy,
well-named methods. Let’s take a quick look at them.
increments()
Add an auto-incrementing ID to the table, most of your tables will have this!string()
Create a VARCHAR field, string sounds a lot better doesn’t it?
integer()
Add an integer field to the table.
float()
Add a float field to the table.
boolean()
Add a boolean field to the table.
date()
Add a date field to the table.
timestamp()
Add a timestamp field to the table.
timestamps()
Add created_at and updated_at timestamp fields.
text()
Add a text field to the table.
blob()
Add a blob data field to the table.
You can also use the chainable method ->nullable() to allow the field to receive NULL values.
Full details on the parameters available to the above methods can be found in the official
documentation¹.
Great, so now we have created our table! Since we created the table in the up() method, we now
need to DROP the table in the down() method, so that the schema will return to its original form
after a roll-back. Fortunately the Schema class has another method that is perfect for this task.
1 <?php
2
3 Schema::drop('users');
Yep, it doesn’t get easier than that. The Schema class also has functions for performing other
schema tasks, such as dropping columns drop_column(), adding indexes unique() and a number
of foreign key methods. I think that covering all of them here would turn the book into an API,
something I don’t wish to do, besides they are explained well in the official documentation², and
if you take a look there we can move on to new things. How about finding out how we run these
migrations? Let’s do it!
Before we can run our migrations, we need to install the laravel_migrations table so that
Laravel can keep track of which migrations have been run. Fortunately artisan has a command
which will perform the necessary database changes for you. Let’s have a go.
1 php artisan migrate:install
And the result..
1 Migration table created successfully.
Great! Now that the laravel_migrations table has been created we can finally run our new
migration. Simply use the task name this time, here we go.
1 php artisan migrate
2 Migrated: application/2012_03_30_220459_create_users
Whoop! If we take a look at our database we will see that the users table has been created
successfully. Wait, we made a mistake! (we didn’t, but I like drama and I need an excuse to use
roll-back) Let’s roll-back our schema to redo the changes.
1 php artisan migrate:rollback
2 Rolled back: application/2012_03_30_220459_create_users
Please note that if you would like to provide example data to populate your database with, simply
create it with the Fluent Query Builder which will be covered in a later chapter.
Easy as pie! Now that you know how to use migrations, you can build your schema and populate
your database whenever you please! In the next chapter we will be looking at the Fluent Query
Builder.
And now, as promised, a list of names / messages from those who have purchased the book for
more than $9.99.
Please don’t forget to talk about the IoC container and how this can be useful with
unit tests. You made a great choice with Laravel. - Jorge Martinez
Thanks for buying the book Jorge, we will be covering the IoC container in a later section, once
we have covered all the basics of app building.
We at @HelpSpot really support what you’re doing to expand the Laravel community,
keep up the great work! - Ian Landsman
Thanks Ian! We the Laravel community really support what you’re doing to expand the
Framework, so now we are even! For those of you who do not know, Ian Landsman is the
head of UserScape, a company which provided the framework’s author with a job that lets him
expand the framework. UserScape also sponsor the bundle site for Laravel, and many other
things! Please check out their product Helpspot, a wonderfully flexible and affordable piece of
help desk software³!
³http://www.helpspot.com/
Migrations 47
There were some other purchases over $9.99 that I have not received an email from yet, remember
if you pay more than $9.99 and you would like your name to appear in the book, please send an
email to me@daylerees.com , unfortunately I can’t see the names of those who purchased on the
sales tab.

 

 
 
 

osdyui

Skills    Laravel

Qualifications :-
Location :-,,,
Description:-
Explore
 

  Students (0)