# Shop: detail section

tracks

# Get selected record

  • We will use the wire:click attribute on the Show Tracks icon to pass the id of the selected record to the showTracks method in the component
  • The showTracks method will retrieve the record from the database and store it in a new public property $selectedRecord
  • Instead of only the id we want to store the complete record in the public property $selectedRecord
  • The long way is to query the database for the record with the id and store the result in the public property $selectedRecord
  • But there is a better way: we can use Laravel's Route Model Binding (opens new window) to get the complete record with only the id as a parameter in the method showTracks

# Add a modal to the view

  • Now we have the complete record in the public property $selectedRecord, we can use it to pop up a modal
  • Let's start with only the artist, the title of the album and the cover image

# Show and hide the modal with Alpine

  • Luckily for us, Alpine and Livewire work together very well (both frameworks are from the same author)
  • With @entangle we can sync a public property from the Livewire component with an Alpine variable
    • When a property in Livewire changes, the Alpine variable changes too
    • When the Alpine variable changes, the Livewire property changes too
  • This is the technique we're going to use to show and hide the modal

# Get all tracks for the selected record

# Show tracks in the modal

dd

  • Don't forget to comment the dump in Livewire/Shop.php!

# Reformat the track length with Carbon

  • The track length is in milliseconds, so we need to convert it to mm:ss
  • You can make a custom function for it that handles the transformation, but we prefer to use the Carbon (opens new window) package for this
    • Carbon is an easy to use PHP extension for date/time manipulation and formatting
    • Carbon is by default installed in in every Laravel project
  • The Carbon method we need is createFromTimestampMs($milliseconds)->format('i:s') (opens new window)
    • createFromTimestampMs creates a Carbon object from a timestamp in $milliseconds
    • format formats the Carbon object to a string
Last Updated: 11/9/2022, 12:46:12 PM