# Admin: covers (part 1)

  • In this chapter, we can edit the covers of a particular record
    • Delete the cover
    • Upload a new cover
    • Reset the cover by downloading it from the MusicBrainz API
  • In the next chapter, we list all the redundant covers in our storage and delete them

WARNING

Even though you can perfectly separate the two functionalities on two different pages, we will keep them on the same page to demonstrate how route-parameters works and how to use those parameters in the Livewire component

# Preparation

# Create a Covers component

  • Create a new Livewire component with the terminal command php artisan make:livewire Admin/Covers
    • app/Http/Livewire/Admin/Covers.php (the component class)
    • resources/views/livewire/admin/covers.blade.php (the component view)
  • Open the component class and change the layout to layouts.vinylshop (leave the title empty)





 
 
 
 



class Covers extends Component
{
    public function render()
    {
        return view('livewire.admin.covers')
            ->layout('layouts.vinylshop', [
                'description' => 'Manage album covers',
                'title' => '',
            ]);
    }
}
1
2
3
4
5
6
7
8
9
10
11

# Add a new route

  • Add a new get-route for the Covers class to the routes/web.php file
  • Update the navigation menu in resources/views/components/layouts/nav-bar.blade.php

ROUTE-PARAMETER

  • In our route, we have a variable id that acts as a route-parameter {id?} (see: route-parameters (opens new window))
  • The ? means that the parameter is optional
  • This means that the route can be called with or without a parameter, e.g.:
    • http://vinyl_shop.test/admin/covers/12 (with parameter)
    • http://vinyl_shop.test/admin/covers (without parameter)
  • You can name the parameter whatever you want, but keep in mind that you have to use the same name in the Livewire component otherwise the parameter will not be passed

# Basic scaffolding for the view

  • Open the resources/views/livewire/admin/covers.blade.php file
  • Replace the content of file with the following code:

TIPS

  • Don't worry about deleting some covers that belong to a record. You can always get them back from the URL http://vinyl_shop.test/download_covers.php (opens new window) (See: chapter Record covers)
  • Refresh the database with the command php artisan migrate:fresh
  • (Optional) add one or more records without a cover to the database, e.g mb_id:
    • a36348b3-1950-49fe-b895-49f586afc895 (Daan - Le Franc Belge)
    • ff6284f3-d5f3-4a13-b839-d64a468aa430 (Lidia Lunch - Queen of Siam)
    • 58dcd354-a89a-48ea-9e6e-e258cb23e11d (Ramones - End of the Century)
    • 794c6bf2-3241-416f-9b8f-24e2d84a1c4b (The Stooges - Fun House)
  • Open the file resources/views/livewire/admin/records.blade.php

# Get the selected record

  • The mount() method of the Covers component has a parameter $id with a default value of null
    • If the parameter is not null, the mount() method will get the record with the given id from the database
    • If the parameter is null, the mount() method don't do anything for now (see part 2)

`find()` vs `findOrFail()`

  • To select one single record from the database, you can use the findOrFail() method or the find() method
  • If you try to select a record that doesn't exist:
    • the findOrFail() method will throw a 404 exception
    • the find() method will return null
  • Go to the URL http://vinyl_shop.test/admin/covers/999 (opens new window) and look at different results with find() and findOrFail()

# Upload a new cover

# Open the modal

# Upload a new cover

  • Livewire makes it super easy to upload files (opens new window) (not only images) and even shows a preview of the file
  • First add the WithFileUploads trait to the Covers component
  • Now you can use the wire:model attribute to bind the newCover property to the input element with the type="file" attribute
  • The uploaded file has a random name and will be stored in the storage/app/livewire-temp directory
  • You can now access the temporary file with $newCover->temporaryUrl()

WARNING

  • Every time you upload a new file, a new temporary file will be created and stored in the storage/app/livewire-temp directory
  • In the screenshot below you can see that I temporarily uploaded 4 files, and they are all stored on the server, in the storage/app/livewire-temp directory Show temp files
  • Livewire will automatically delete the temporary files after 24 hours, but you can also delete them manually

# Save the cover

  • The SAVE button will only be visible if the newCover property contains a value

WARNING

  • If the old cover is still displayed, you can try to clear the cache with the CTRL + SHIFT + R key

# Avoid browser caching

  • When you upload a new cover, the browser will cache the image and probably won't show the new cover because it thinks it's already in his cache (the name of the file is not changed, only the content)
  • To avoid this, you can add a random string to the end of the image url, eg: the current timestamp
  • Update the src attribute of the img tag in the livewire/admin/covers.blade.php file
    • from: $record->cover['url']
    • to: $record->cover['url'] . '?' . time()


 












<div x-data="" class="w-60 h-60 flex flex-col gap-4">
    <img class="border border-gray-300 object-cover rounded shadow-lg"
         src="{{ $record->cover['url'] . '?v=' . time() }}"
         alt="">
    <div class="border border-gray-500 flex text-center [&>a]:flex-1 [&>a]:bg-gray-300 [&>a]:p-2 [&>a]:transition">
        <a href="#"
           wire:click.prevent="openModal()"
           class="hover:text-white hover:bg-sky-800 border-r border-gray-500">EDIT</a>
        <a href="#"
           class="hover:text-white hover:bg-green-800 border-r border-gray-500">RESET</a>
        <a href="#"
           class="hover:text-white hover:bg-red-800">DELETE</a>
    </div>
</div>
1
2
3
4
5
6
7
8
9
10
11
12
13
14

# Reset the cover

  • When the RESET button is clicked, the resetCover() method will be called

# Add a preloading text

  • Fetching data from an external API can take a while, so it's a good idea to show a preloading text

# Delete the cover

  • When the DELETE button is clicked, the deleteCover() method will be called after a confirmation message
Last Updated: 3/6/2023, 8:01:02 PM