11.08.2017 Views

codebright

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

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

Eloquent Relationships 347<br />

Let’s build two example tables.<br />

books<br />

1 +---------+------------------------+<br />

2 | id (PK) | name |<br />

3 +---------+------------------------+<br />

4 | 1 | Code Sexy |<br />

5 | 2 | Code Dutch |<br />

6 | 3 | Code Bright |<br />

7 +----------------------------------+<br />

Here’s our Book table with three books held within. You will notice that each table has a unique<br />

integer primary key as required by the Eloquent ORM. This can be used to identify each individual<br />

row. Now let’s take a look at the second table.<br />

authors<br />

1 +---------+------------------------+<br />

2 | id (PK) | name |<br />

3 +---------+------------------------+<br />

4 | 1 | Dayle Rees |<br />

5 | 2 | Matthew Machuga |<br />

6 | 3 | Shawn McCool |<br />

7 +----------------------------------+<br />

Here we have another table containing the names of three fantastically handsome developers. We<br />

will call this the Author table. Notice how each row once again has an integer primary key that can<br />

be used as a unique identifier?<br />

Right, let’s form a relationship between the two tables. This is a relationship between a Book and<br />

an Author, we will ignore co-authorship for now and assume that a book will only have a single<br />

author. Let’s add a new column to the Book table.<br />

books

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

Saved successfully!

Ooh no, something went wrong!