Skip to main content

Comments & Discussion

AA
Ali Al Qahtani ✓ Link copied!

I think we need to remove the image when deleting the activity

I suggest this in destroy method:

public function destroy(Company $company, Activity $activity)
{
$this->authorize('delete', $company);
 
if ($activity->photo) {
$this->unlinkPhoto(image: $activity->photo, disk: 'activities');
}
 
$activity->delete();
 
return to_route('companies.activities.index', $company);
}
private function unlinkPhoto($image, $disk = 'public')
{
Storage::disk($disk)->delete([$image, 'thumbs/' . $image]);
}
A
Alexey ✓ Link copied!

Hello. You forgot to describe the ActivityObserver file.

J
jwinder ✓ Link copied!

THis has to be outdated with Laravel 10. Ive tried everything to get use Intervention\Image\Facades\Image recognized, and its just not working for the life of me.

N
Nerijus ✓ Link copied!

No it is outdated as it was made with laravel 10. Can you be more specific what isn't working? What errors you get?

J
jwinder ✓ Link copied!

Error: Class "Intervention\Image\Facades\Image" not found in /Applications/XAMPP/xamppfiles/htdocs/reservations/app/Http/Controllers/CompanyActivityController.php:105 This happens whenever I run my CompanyActivityTest.Test_can_upload_image wont pass because Image::make in the CompanyActivitiyController isn't recognized.

Ive tried adjusting providers array in app.php, as well as aliases, ive tried php artisan cache:clear, and config, tried php artisan optimize:clear, composer dump-autoload, verifiying its installed, reinstalling it, pretty much everything.

N
Nerijus ✓ Link copied!

Can you properly format your comments?

J
jwinder ✓ Link copied!

Well i think i finally solved it. I was installing intervention/image version 3.4. They totally changed how it works if you visit their Github (looking at the README.md: https://github.com/Intervention/image . So I looked at their revision history and tried using version 3.0 , still no dice. I then decided to use version 2.7. That fixed it, although i got a bunch of nasty warnings when i updated my composer.json intervention/image in "require" to 2.7. But that fixed it. I think the syntax might need updating to the modern version that Github reflects, however i could be wrong.

N
Nerijus ✓ Link copied!

You are rigth. If you would look at the repository at the time of writing the 2.7 version was used. Maybe this course will get updated with the release of Laravel 11.

T
teebee ✓ Link copied!

Anyone with php8.3 on Windows, did you managed to pass the "can upload test"? I didn't have imagick enabled, so I embarked on a quest to get it enable, and it looks like is not working with php 8.3 yet.

FAILED Tests\Feature\CompanyActivityTest > can upload image Unable to find a file or directory at path [thumbs/HQEcxv5QkPGmBwXYymJsmMrGSgYw5EIvWKq638wl.jpg]. Imagick PHP extension must be installed to use this driver.

at vendor\laravel\framework\src\Illuminate\Filesystem\FilesystemAdapter.php:119 115▕ 116▕ $paths = Arr::wrap($path); 117▕ 118▕ foreach ($paths as $path) { ➜ 119▕ PHPUnit::assertTrue( 120▕ $this->exists($path), "Unable to find a file or directory at path [{$path}]." 121▕ ); 122▕ 123▕ if (! is_null($content)) {

1 vendor\laravel\framework\src\Illuminate\Filesystem\FilesystemAdapter.php:119 2 tests\Feature\CompanyActivityTest.php:87

Tests: 1 failed, 13 passed (31 assertions) Duration: 4.25s

I do have php 8.1 installed, but since I started the tutorial with php 8.3 I can't switch back and forth. I'm curios if someone in my situation managed to move forward.

Thanks!

N
Nerijus ✓ Link copied!

You must enable the extension otherw it won't

T
teebee ✓ Link copied!

That's a bummer :( . Looks like the support for imagick on php 8.3 still requries some development efforts form php dev team, and I really don't want to start from scratch under a different version.

N
Nerijus ✓ Link copied!

I really doubt its the extensions problem. It's how to enable it. And you don't need to start over just because of php version. But laravel 11 requires 8.2 minimum if you are using that version

T
teebee ✓ Link copied!

Can you expand on the part where I don't need to start over if I switch to php 8.1? I am interested to move forward without having write the whole thing from ground up, but when I tried to switch from php8.3 to php8.1, and atempted to run php artisan serve I had a different error requiring at least php 8.2. The Laravel version I'm using is 10.3.

N
Nerijus ✓ Link copied!

Run composer update and you should be fine. But I would suggest figur the 8.3 problem as 8.1 isn't maintained anymore

T
teebee ✓ Link copied!

I managed to make it work on MacOS with Laravel 10 and php 8.3, now all test are passing with flying colors. I will try your suggestion on Windows tomorrow as I don't access to one from home. Thank you.

T
teebee ✓ Link copied!

Maan, this was a long journey. There is no official release of imagick for PHP 8.3 due to issues with the build machine (according to their statement on Github) however there are a few nice people that made it their mission to build imagick for PHP 8.3 and can be downloaded form here: https://github.com/Imagick/imagick/issues/573#issuecomment-1827616798 Now I have all test passing on Windows as well.

D
davidvm ✓ Link copied!

@teebee thanks for your link. I made it work on my Windows 10 machine in PHP 8.3.4 with Laragon.

T
teebee ✓ Link copied!

I knew someone else was bound to run into the same issue :). Glad to hear it helped you.

D
davidvm ✓ Link copied!

If have an issue displaying the thumbnails. I configured filesystems.php as in this lesson, but my files are getting stored in public/storage/activities instead of public/activities. For this reason the images don't show up.

I could fix this by changing the method to:

protected function thumbnail(): Attribute
{
return Attribute::make(
get: fn () => $this->photo ? '/storage/activities/thumbs/' . $this->photo : '/noimage.png',
);
}

Or by changing following in the the filesystems.php

'root' => storage_path('app/public/activities')

to

'root' => public_path() . '/activities',
  1. Could there be something wrong and is that the reason why it uses this subfolder storage?
  2. Which fix is the best? I see public_path() only stores the file in the public path in a none linked folder, but not in the storage folder.
M
Modestas ✓ Link copied!

You should not store images in the public folder. Your public folder should have a symlink called storage there (after running php artisan storage:link) and you should access all of your images from /storage/XXX/XXX.png.

Changing your filesystem to write to public path is really bad as that will push all images to GIT, which will cause issues.

D
davidvm ✓ Link copied!

Hi Modestas, thank you for your reply. Writing to the public path seemed indeed as a bad practice. I do have a storage folder in my public folder, but in the code above the activities folder is a direct child of the public folder. Maybe its from an older Laravel version.

M
Modestas ✓ Link copied!

I think that you are misunderstanding things a little bit :)

/public/storage will symlink to /storage/app/public - which means that all the uploads can be accessed publicly.

There's another, private storage, which would write into /storage/app and it would not be accessible via url. It will be uploaded, but there will not be any access from URL endpoint.

So to answer your question - you have to upload all your files to /storage/app/public which will be accessible via domain.com/storage/PATH_TO_FILE

D
davidvm ✓ Link copied!

That was what I meant, but clearly I haven't explained it correctly. We are on the same wavelength. Thank you

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.