# Shop: master section

REMARKS

  • Because this is our first Livewire component in this course, we will split the solution in 3 parts:
    • Part 1: let's start with the "master page" where all the records are shown on a card and add some pagination
    • Part 2: when the user clicks on a record, a modal pops up with some extra details (the tracks) of the record
    • Part 3: finally, we'll add some filtering (by name, by price and by genre) and sorting
  • How to actually order a record in the shop is a bit advanced and will be explained in a later chapter

# Preparation

# Create the Shop component

  • Make a new Livewire component with the terminal command php artisan make:livewire Shop
  • This command creates two files:
    • app/Http/Livewire/Shop.php (the component class contains the logic that was previously in the controller)
    • resources/views/livewire/shop.blade.php (the view)
  • Check the following code to add the component to the resources/views/shop.blade.php view:

# Add a new route

  • Add a get-route to the routes/web.php file
  • Update the navigation menu in resources/views/components/layout/nav.blade.php

# Reference the vinylshop layout template

  • As for now, we get an error when we navigate to the shop page
  • By default, Livewire will render the component into the $slot of a blade layout component located at: resources/views/layouts/app.blade.php
  • To fix this error, we must add a reference to the appropriate template (layouts/vinylshop.blade.php) to the view

# Get all records

  • It's time to get all the records and render a card for each one of them
  • Getting the records is achieved INSIDE the render() method in the component, and you pass them, with the compact() methode, to the view
  • Loop through the records in the view and display them all

IMPORTANT NOTICE ABOUT LIVEWIRE LOOPS

  • ALWAYS use the wire:key directive inside a loop to make sure that Livewire can keep track of the records
    • This is necessary because the records are displayed in a loop
    • If you don't use the wire:key directive, Livewire will not be able to keep track of the records and will not be able to update them
    • The wire:key directive is a unique identifier for each record
    • In this case, we could use the id or the mb_id attribute of the record

# Update the card

TIP

  • If you can't remember the attributes of a model, you can (temporary) use:
    • dd($records); or dd($records->toArray()); to dump and die the recordset, but this will stop the execution of the code
    • dump($records->toArray()); to just dump the recordset as an array to the screen, without stopping the execution of the code
    • or use dump($records->toArray()[0]); to dump only the first element of the recordset

# Add the correct information to the card

  • Update the card within the foreach loop with the correct information about the record

# Hide the order button when not in stock

  • Showing the order button is only relevant when the record is in stock
  • Else we should hide it and replace it with a message SOLD OUT

# Pagination

  • Showing all the records on one page is not a good idea, so we will add pagination (opens new window) to the page
  • We add a public property $perPage to the component to keep track of the number of records per page
    (We'll change this value later in the view to make it more dynamic)
  • The default pagination links are not very nice, so we will change the default layout and highlight the current page with a darker background color
  • Before we can do this we need to pull Livewires pagination component out of the framework and into our project
    • This is done by running the following command in the terminal: php artisan livewire:publish --pagination
    • This will copy the 4 pagination views to the resources/views/vendor/livewire folder
      (You can safely delete three of them because we only need the tailwind.blade.php view)

# Add a preloader

  • Switching between pages is very fast (every click is a round-trip to the server), but it is still nice to show a preloader while the page is updating in the background

# Add a tooltip to the buttons

Last Updated: 11/10/2022, 7:26:58 AM