DEV Community

Arif Iqbal
Arif Iqbal

Posted on

Week 4 of 100DaysOfCode Laravel Challenge

This week I had a chance to discover more about Laravel's Eloquent Model. I learned many concepts including Route-Model binding, Eloquent relationships (used belongsTo() and hasMany() out of the available) relationships, the N+1 problem, debugging Laravel using Log façade or the Clockwork package.

Below is a recap of my week in the form of separate posts.

  • Laravel's route model binding feature allows us to bind a route wildcard to an Eloquent model instance.
  • Our next job is to figure out how to assign a category to each post. To allow for this, we'll need to create a new Eloquent model and migration to represent a Category.
  • Now that we have the concept of a Category in our application, let's make a new route that fetches and loads all posts that are associated with the given category.
  • We introduced a subtle performance issue in the last episode that's known as the N+1 problem. Because Laravel lazy-loads relationships, this means you can potentially fall into a trap where an additional SQL query is executed for every item within a loop. Fifty items...fifty SQL queries. In this episode, we debug these queries - both manually, and with the excellent Clockwork extension - and then we solve the problem by eager loading any relationships we'll be referencing.

Discussion (0)