Assignment 5

Rebuilding the Albums Module

Rebuild the Albums module exclusively using Eloquent. For the Albums page (/albums), be sure to eager load the artists to improve performance.

Feel free to skip sorting by artist name. That is a bit more complicated because it involves ordering by a relationship. If you are interested in the solution to that, here is the code:

$albums = Album::join('artists', 'artists.id', '=', 'albums.artist_id')
  ->with(['artist'])
  ->orderBy('artists.name')
  ->orderBy('title')
  ->select('*', 'albums.id as id')
  ->get();

In order to sort by the related artist name, you need to use a join like you did with the query builder. However, doing a join messes up the album ID and points to the artist ID. To fix that, you can use the select method to reset the album ID.

Add a link to this version of the module in the main navigation with the text “Albums (Eloquent)“. Feel free to name your routes, controllers, and views however you wish. What will be graded for this assignment is that you got it working using Eloquent and had no DB calls.

If you’d like extra practice working with Eloquent, give the following challenges a try.

Extra Eloquent Practice (optional)

  • Recreate Assignment 2 with Eloquent. You should have a many-to-many relationship between the Playlist model and the Track model.
  • Recreate Assignment 3 with Eloquent.
  • Eloquent problems

    1. Find all tracks with the genre “Metal”.
    2. Find all customers who work for “Apple Inc.”.
    3. Find invoice 5 and update its BillingAddress to “123 Sesame Street”.
    4. What is the total of all invoices in 2012?
    5. What is the average track length?

For the last 2, check out the documentation on Retrieving Aggregates.

Submission

Verify that your app has been deployed to Heroku, which you set up in Assignment 2.

Send an email to Brighton and me with the URL to your GitHub repository with the subject: “ITP 405 - Assignment 5”. This should be the same URL that you submitted before. Sending us an email helps us keep track of assignment submissions.