CONTENTS
Creating the post_tag . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
Recreating the full database structure . . . . . . . . . . . . . . . . . . . 103
Query Builder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
Consulting the SQL generated by the Query Builder . . . . . . . . . . 105
Returning all records in the table . . . . . . . . . . . . . . . . . . . . 105
Stepping through the records after the query . . . . . . . . . . . . . . 107
Returning the first row of a table . . . . . . . . . . . . . . . . . . . . 107
Returning a list of values from a table . . . . . . . . . . . . . . . . . . 108
Aggregating values . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
Selecting fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
Joins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
Unions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
Where . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
Grouping Wheres . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
Order . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
GroupBy e Having . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
Logging Eloquent’s SQLs . . . . . . . . . . . . . . . . . . . . . . . . . . 113
Eloquent ORM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
Creating the model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
Agreements between tables and model . . . . . . . . . . . . . . . . . . . 117
Use of the Timestamp . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
Testing the User model . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
Routing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
Create the method on the controller . . . . . . . . . . . . . . . . . . . 119
Understanding the UserController . . . . . . . . . . . . . . . . . . . . 120
Relationships in Eloquent . . . . . . . . . . . . . . . . . . . . . . . . . . 121
Relationships between users and posts . . . . . . . . . . . . . . . . . 121
Relationship between Posts and Comments . . . . . . . . . . . . . . . 124
Relationship between Posts and Tags . . . . . . . . . . . . . . . . . . 126
One-to-one relationship . . . . . . . . . . . . . . . . . . . . . . . . . 129
One-to-many relationship through (Has Many Through) . . . . . . . 131
Analyzing the existence of records in a relationship . . . . . . . . . . 132
Eager Loading vs Lazy Loading and the N+1 problem . . . . . . . . . . . 133
Eager Loading and more advanced queries . . . . . . . . . . . . . . . . . 134
Inserting and updating records . . . . . . . . . . . . . . . . . . . . . . . 139