03.12.2015 Views

laravel4cookbook

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

API 120<br />

.<br />

Pay special attention to the names of the tables in the calls to the on() method. I have changed them<br />

from plural tables names to singular table names. The templates used to construct these constraints<br />

don’t seem to take into account the name of the table on which the constrains are being created.<br />

Apart from the integer fields (which we’ve seen before); these pivot tables also have foreign keys,<br />

with constraints. These are common features of relational databases, as they help to maintain<br />

referential integrity among database entities.<br />

.<br />

These migrations specify foreign key constraints. This means the database will require valid foreign<br />

keys when you insert and update rows in these tables. Your seeder class needs to provide these valid<br />

ID values, or you should remove the constraints (references(‘id’)…onDelete(‘cascade’)).<br />

With all these migration files created; we have only to migrate them to the database with:<br />

1 php artisan migrate<br />

(This assumes you have already configured the database connection details, in the configuration<br />

files.)<br />

Generating Seeders<br />

Seeders are next on our list. These will provide us with starting data; so our API responses don’t<br />

look so empty.<br />

1 php artisan generate:seed Category<br />

2 php artisan generate:seed Sponsor<br />

These commands will generate stub seeders, and add them to the DatabaseSeeder class. I have gone<br />

ahead and customised them to include some data (and better formatting):

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

Saved successfully!

Ooh no, something went wrong!