Laravel-medialibrary: image won't load

Created on 18 Oct 2018  Â·  7Comments  Â·  Source: spatie/laravel-medialibrary

Hi there, I've been trying to show the image on view, but I can't seem to get it to work

I get this on console log

GET http://uw-test.test/storage/1/AddStock2.PNG 404 (Not Found)

I've been trying lots of stuff to show the image, but I can't seem to make it work, clearly I must be missing something. Any help is great

This is my controller
$products = Product::select('products.*')
->keywords(request('search'))
->join('categories', 'categories.id', '=', 'products.category_id')
->category(request('category_id'))
->with('media')
->paginate(config('fi.resultsPerPage'));

This is dd of products
LengthAwarePaginator {#698 â–¼
#total: 5
#lastPage: 1
#items: Collection {#712 â–¼
#items: array:5 [â–¼
0 => Product {#703 â–¶}
1 => Product {#705 â–¶}
2 => Product {#702 â–¶}
3 => Product {#701 â–¶}
4 => Product {#700 â–¼
#guarded: array:1 [â–¶]
#connection: "mysql"
#table: null
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:26 [â–¶]
#original: array:26 [â–¶]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: array:1 [â–¼
"media" => Collection {#691 â–¼
#items: array:1 [â–¼
0 => Media {#697 â–¼
#guarded: []
#casts: array:3 [â–¶]
#connection: "mysql"
#table: null
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:15 [â–¼
"id" => 1
"model_type" => "FI\Modules\Products\Models\Product"
"model_id" => 5
"collection_name" => "products"
"name" => "AddStock2"
"file_name" => "AddStock2.PNG"
"mime_type" => "image/png"
"disk" => "public"
"size" => 64212
"manipulations" => "[]"
"custom_properties" => "{"custom_headers": []}"
"responsive_images" => "[]"
"order_column" => 1
"created_at" => "2018-10-18 20:41:48"
"updated_at" => "2018-10-18 20:41:48"
]
#original: array:15 [â–¶]
#changes: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#fillable: []
}
]
}
]
#touches: []
+timestamps: true
#hidden: []
#visible: []
#fillable: []
+mediaConversions: []
+mediaCollections: []
#deletePreservingMedia: false
#unAttachedMediaLibraryItems: []
}
]
}
#perPage: "15"
#currentPage: 1
#path: "http://uw-blb.test/products"
#query: []
#fragment: null
#pageName: "page"
}

My model is like this
use Spatie\MediaLibrary\HasMedia\HasMedia;
use Spatie\MediaLibrary\HasMedia\HasMediaTrait;

class Product extends Model implements HasMedia
{
use Sortable;
use HasMediaTrait;

public function registerMediaCollections()
{
$this->addMediaCollection('products');

}

This is my ProductCreateController
if ($request->has('attachments')) {
foreach ($request->attachments as $key => $imagen) {
try {
$product->addMedia($request->attachments[$key])->toMediaCollection('products');
} catch (DiskDoesNotExist $e) {
} catch (FileDoesNotExist $e) {
} catch (FileIsTooBig $e) {
}
}
}

Most helpful comment

If you use homestead, you need to use the php artisan storage:link command in vm.

  1. unlink storage in your host OS
  2. vagrant ssh
  3. php artisan storage:link

You can get more details from https://github.com/laravel/framework/issues/15439

All 7 comments

Hi I see two different server domain names in your comments are you pulling your images from the same server or is there a problem with your generated path names?

Hmm nah apparently that's not the issue, was thinking that might have been the issue, but nah its not apparently.

I'm on http://uw-blb.test/products

Its the same path
products:770 GET http://uw-blb.test/storage/2/AllOrders.PNG 404 (Not Found)
products:731 GET http://uw-blb.test/storage/1/AddStock2.PNG 404 (Not Found)
products:848 GET http://uw-blb.test/storage/4/Ballina.PNG 404 (Not Found)
products:809 GET http://uw-blb.test/storage/3/createVendor.PNG 404 (Not Found)

Hi have you run php artisan storage:link?

Dale Marthaller([email protected])

----- Original Message -----
From: MyhedinZika notifications@github.com
To: spatie/laravel-medialibrary laravel-medialibrary@noreply.github.com
Cc: DMarthaller dmarthaller@shaw.ca, Comment comment@noreply.github.com
Sent: Sat, 20 Oct 2018 11:32:40 -0600 (MDT)
Subject: Re: [spatie/laravel-medialibrary] image won't load (#1275)

Hmm nah apparently that's not the issue, was thinking that might have been the issue, but nah its not apparently.

I'm on http://uw-blb.test/products

Its the same path
products:770 GET http://uw-blb.test/storage/2/AllOrders.PNG 404 (Not Found)
products:731 GET http://uw-blb.test/storage/1/AddStock2.PNG 404 (Not Found)
products:848 GET http://uw-blb.test/storage/4/Ballina.PNG 404 (Not Found)
products:809 GET http://uw-blb.test/storage/3/createVendor.PNG 404 (Not Found)

--
You are receiving this because you commented.
Reply to this email directly or view it on GitHub:
https://github.com/spatie/laravel-medialibrary/issues/1275#issuecomment-431602524

Yes I have tried that, but unfortunately thats not the problem

The "public/storage" directory already exists.

If you copy the generated IMG url in another browser window can you see the image? If you either have a misgenerated url or a server config (missing link from your public directory to the storage directory issue).

Dale Marthaller([email protected])

----- Original Message -----
From: MyhedinZika notifications@github.com
To: spatie/laravel-medialibrary laravel-medialibrary@noreply.github.com
Cc: DMarthaller dmarthaller@shaw.ca, Comment comment@noreply.github.com
Sent: Sat, 20 Oct 2018 13:58:47 -0600 (MDT)
Subject: Re: [spatie/laravel-medialibrary] image won't load (#1275)

Yes I have tried that, but unfortunately thats not the problem

The "public/storage" directory already exists.

--
You are receiving this because you commented.
Reply to this email directly or view it on GitHub:
https://github.com/spatie/laravel-medialibrary/issues/1275#issuecomment-431613434

@MyhedinZika I had a similar situation happen on my site. It wasn't until I read the comment by @Dmarthaller that I realized I might have an issue with my storage link.
It turns out that when I moved my site to a different directory the symlink didn't transfer correctly. It ended up making a copy of the 'storage' directory instead of remaining a symlink.
I even ran php artisan storage:link and it came up with 'The "public/storage" directory already exists.'

I decided to double check that the public/storage directory did in fact exist. That's when I noticed it wasn't a symlink but an actual directory. After making a quick backup of my site I removed the 'public/storage' directory and ran php artisan storage:link again. Once the function completed all my images started loading as expected.

If you use homestead, you need to use the php artisan storage:link command in vm.

  1. unlink storage in your host OS
  2. vagrant ssh
  3. php artisan storage:link

You can get more details from https://github.com/laravel/framework/issues/15439

Was this page helpful?
0 / 5 - 0 ratings

Related issues

snapey picture snapey  Â·  3Comments

brendt picture brendt  Â·  4Comments

drtheuns picture drtheuns  Â·  3Comments

Krato picture Krato  Â·  4Comments

swash13 picture swash13  Â·  3Comments