Appearance
Admin: covers (part 2)
PODCAST
- If, for whatever reason, any covers remain in storage, we are going to show and delete them in this chapter
- Remember that this chapter only works when the route contains no parameter:
http://vinyl_shop.test/admin/covers/ - Maybe it's best to start with a clean site and install the original covers again:
- Select all files in the
storage/app/public/covers
folder and delete them - Open http://vinyl_shop.test/admin/download_covers in the browser to download the original covers
- Select all files in the
Get all the covers
- Let's start with fetching all the covers from the disk and show them in a grid
- Line 20: get all the files from the
covers
folder - Line 21: store the result in the
$redundantCovers
property
php
class Covers extends Component
{
use WithFileUploads;
public $showModal = false;
public $record = null;
#[Validate('required|image|mimes:jpg,jpeg,png,webp|max:1024')]
public $newCover;
public $redundantCovers = [];
...
public function mount($id = null)
{
if ($id) {
// get the selected record if id is not null
$this->record = Record::findOrFail($id);
} else {
// get all the redundant covers from the disk
$covers = Storage::disk('public')->files('covers');
$this->redundantCovers = $covers;
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Remove the dummy cover
- Line 8: remove the
no-cover.png
from the array
php
public function mount($id = null)
{
if ($id) { ... }
else {
// get all the redundant covers from the disk
$covers = Storage::disk('public')->files('covers');
// remove no-cover.png from the array
$covers = array_diff($covers, ['covers/no-cover.png']);
$this->redundantCovers = $covers;
}
}
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
Get all the covers from the database
- Line 10: get all the records from the database
- Line 11: initialize an empty array where we will store the paths to the covers
- Line 12 - 14: loop over the records and add the path to the cover to the array
- Line 15: dump the array to the browser (temporary)
php
public function mount($id = null)
{
if ($id) { ... }
else {
// get all the redundant covers from the disk
$covers = Storage::disk('public')->files('covers');
// remove no-cover.png from the array
$covers = array_diff($covers, ['covers/no-cover.png']);
// get all the covers from the records table
$records = Record::get();
$dbCovers = [];
foreach ($records as $record) {
$dbCovers[] = 'covers/' . $record->mb_id . '.jpg';
}
dump($dbCovers);
$this->redundantCovers = $covers;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Remove used covers from the array
- Line 15: hide (or remove) the temporary dump
- Line 17: limit
$covers
to the covers that are not in the$dbCovers
array
php
public function mount($id = null)
{
if ($id) { ... }
else {
// get all the redundant covers from the disk
$covers = Storage::disk('public')->files('covers');
// remove no-cover.png from the array
$covers = array_diff($covers, ['covers/no-cover.png']);
// get all the covers from the records table
$records = Record::get();
$dbCovers = [];
foreach ($records as $record) {
$dbCovers[] = 'covers/' . $record->mb_id . '.jpg';
}
// dump($dbCovers);
// limit $covers to the covers that are not in the $dbCovers array
$covers = array_diff($covers, $dbCovers);
$this->redundantCovers = $covers;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Delete the cover
- We already have a delete method from the previous chapter, so we can refactor it a bit
- Line 3: add a parameter
$redundantCover
to the method and give it a default value ofnull
- If
$redundantCover
is:- Line 6 - 6: not
null
, delete the file from the disk and remove the cover from the$redundantCovers
array and - Line 8 - 13: is
null
: this is the same code as before the refactoring
- Line 6 - 6: not
php
// ask for confirmation to delete the cover
public function deleteCover($redundantCover = null)
{
if ($redundantCover) {
Storage::disk('public')->delete($redundantCover);
$this->redundantCovers = array_diff($this->redundantCovers, [$redundantCover]);
} else {
$this->swalConfirm(
'Delete this cover?',
"You can always upload a new cover later",
'white',
'delete-cover',
);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15