# Admin: covers (part 2)

# 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



















 
 





class Covers extends Component
{

    use WithFileUploads;

    public $showModal = false;
    public $record = null;
    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;
        }
    }

}
Copied!
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

# Remove the dummy cover

  • Line 8: remove the no-cover.png from the array






 
 




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;
    }
}
Copied!
1
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)








 
 
 
 
 
 
 




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;
    }
}
Copied!
1
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














 
 
 




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;
    }
}
Copied!
1
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 1: add a parameter $redundantCover to the method and give it a default value of null
  • Line 3 - 5: if $redundantCover is not null, remove the cover from the $redundantCovers array and delete the file from the disk
  • Line 7 - 8: if $redundantCover is null: this is the same code as before the refactoring
 

 
 
 
 


 


public function deleteCover($redundantCover = null)
{
    if ($redundantCover) {
        $this->redundantCovers = array_diff($this->redundantCovers, [$redundantCover]);
        Storage::disk('public')->delete($redundantCover);
    } else {
        $coverName = 'covers/' . $this->record->mb_id . '.jpg';
        Storage::disk('public')->delete($coverName);
    }
}
Copied!
1
2
3
4
5
6
7
8
9
10
Last Updated: 4/20/2023, 6:14:15 PM