11.08.2017 Views

codebright

Create successful ePaper yourself

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

Eloquent ORM 266<br />

1 mysql> use myapp;<br />

2 Database changed<br />

3 mysql> select * from games;<br />

4 +----+-----------------+------------------------+<br />

5 | id | name | description |<br />

6 +----+-----------------+------------------------+<br />

7 | 1 | Assassins Creed | Assassins VS templars. |<br />

8 +----+-----------------+------------------------+<br />

9 1 row in set (0.00 sec)<br />

We can see that our new row has been inserted correctly. Great! We have inserted a new record<br />

without writing a single line of SQL. Now that’s my kind of victory.<br />

You will notice that we didn’t have to specify an id value for our object. The id column is<br />

automatically incremented, so the database layer will handle the numbering of our rows for us.<br />

It’s generally a bad idea to modify the id column of an Eloquent model. Try to avoid it unless you<br />

really know what you’re doing.<br />

We used the $timestamps attribute to disable automatic timestamps. Instead, let’s take a look at<br />

what happens when we enable them. First we need to alter our database schema. It’s a bad idea<br />

to manually modify our database schema, or two update existing migrations. This is because our<br />

database state might become ‘out-of-sync’ with our teammates. Instead, let’s create a new migration<br />

that our teammates could also execute to receive our changes.<br />

1 $ php artisan migrate:make add_timestamps_to_games<br />

2 Created Migration: 2013_07_14_165416_add_timestamps_to_games<br />

Our migration has been created. You will notice that we gave the migration a discriptive name to<br />

represent our intentions with the migration. This could be useful information should your teammates<br />

later execute the migration. Let’s use the schema builder to add timestamps to our game table.<br />

1

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

Saved successfully!

Ooh no, something went wrong!