Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

return placeholder image if file not exists on storage #61

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/HasImageUploads.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ public function imageUrl(?string $field = null): string
// check for placeholder defined in option
$placeholderImage = Arr::get($this->uploadFieldOptions, 'placeholder');

return (empty($attributeValue) && $placeholderImage)
$exists = !empty($attributeValue) && $this->getStorageDisk()->exists($attributeValue);
return (!$exists && $placeholderImage)
? $placeholderImage
: $this->getStorageDisk()->url($attributeValue);
}
Expand Down
37 changes: 33 additions & 4 deletions tests/ImageUpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,11 +207,40 @@ public function it_uploads_image_by_field_name()
}

/**
* it gives image url if image saved in db
* it gives image url if image saved in db and file exists
*
* @test
*/
public function it_gives_image_url_if_image_saved_in_db()
public function it_gives_image_url_if_image_saved_in_db_and_file_exists()
{
$this->user = new User();
$this->user->setImagesField([
'avatar' => ['placeholder' => '/images/cover-placeholder.png']
]);

$this->user->forceFill([
'name' => 'John',
'email' => 'John@email.com',
'password' => 'secret',
'avatar' => 'uploads/my-avatar.png'
])->save();

Storage::fake('public');
$image = UploadedFile::fake()->image('my-avatar.png', 400, 500);
$this->user->uploadImage($image);

$this->assertEquals($this->user->getOriginal('avatar'), 'uploads/'.$image->hashName());

$this->assertEquals('/storage/uploads/' . $image->hashName(), $this->user->imageUrl());
$this->assertEquals('/storage/uploads/' . $image->hashName(), $this->user->imageUrl('avatar'));
}

/**
* it gives image url if image saved in db and file not exists
*
* @test
*/
public function it_gives_placeholder_url_if_image_saved_in_db_and_file_not_exists()
{
$this->user = new User();
$this->user->setImagesField([
Expand All @@ -227,8 +256,8 @@ public function it_gives_image_url_if_image_saved_in_db()

$this->assertEquals($this->user->getOriginal('avatar'), 'uploads/my-avatar.png');

$this->assertEquals('/storage/uploads/my-avatar.png', $this->user->imageUrl());
$this->assertEquals('/storage/uploads/my-avatar.png', $this->user->imageUrl('avatar'));
$this->assertEquals('/images/cover-placeholder.png', $this->user->imageUrl());
$this->assertEquals('/images/cover-placeholder.png', $this->user->imageUrl('avatar'));
}

/**
Expand Down