Reached said: These are shown perfectly fine in my view, however when I try to access the same images in other views, they fail to show up (404 is returned from the server).
What do you mean? Does the exact url (eg: http://example.com/images/photo.jpg
) work properly in the first view but throw a 404 in the second view, or are you outputting the path only? I suspect that the problem is you're using a relative path, you need to use an absolute path if you different level routes.
For example if you're on example.com
and have <img src="image.jpg">
the web browser will request example.com/image.jpg
whereas if you're on example.com/users/john
and have <img src="image.jpg">
the browser will request example.com/users/image.jpg
.
If that isn't the problem please provide the output of dd($annonce);
and the HTML that is being output by your second view.
Hey citricsquid,
here is the output of dd($annonce):
object(Annonce)[271]
protected 'table' => string 'annonces' (length=8)
protected 'fillable' =>
array (size=7)
0 => string 'title' (length=5)
1 => string 'body' (length=4)
2 => string 'category_id' (length=11)
3 => string 'invest_need' (length=11)
4 => string 'photo' (length=5)
5 => string 'post_author' (length=11)
6 => string 'user_id' (length=7)
protected 'connection' => null
protected 'primaryKey' => string 'id' (length=2)
protected 'perPage' => int 15
public 'incrementing' => boolean true
public 'timestamps' => boolean true
protected 'attributes' =>
array (size=13)
'id' => string '16' (length=2)
'title' => string 'Pølsevognen' (length=12)
'body' => string 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor ' (length=255)
'invest_need' => string '9' (length=1)
'invest_type' => string '' (length=0)
'invest_span' => string '0' (length=1)
'featured' => string '0' (length=1)
'post_author' => string 'Casper' (length=6)
'photo' => string 'images/annoncebilleder/2014-04-25-09:51:07-pc3b8lsevogn-pc3a5-torvet-1.jpg' (length=74)
'user_id' => string '0' (length=1)
'category_id' => string '1' (length=1)
'created_at' => string '2014-04-25 09:51:07' (length=19)
'updated_at' => string '2014-04-25 09:51:07' (length=19)
protected 'original' =>
array (size=13)
'id' => string '16' (length=2)
'title' => string 'Pølsevognen' (length=12)
'body' => string 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor ' (length=255)
'invest_need' => string '9' (length=1)
'invest_type' => string '' (length=0)
'invest_span' => string '0' (length=1)
'featured' => string '0' (length=1)
'post_author' => string 'Casper' (length=6)
'photo' => string 'images/annoncebilleder/2014-04-25-09:51:07-pc3b8lsevogn-pc3a5-torvet-1.jpg' (length=74)
'user_id' => string '0' (length=1)
'category_id' => string '1' (length=1)
'created_at' => string '2014-04-25 09:51:07' (length=19)
'updated_at' => string '2014-04-25 09:51:07' (length=19)
protected 'relations' =>
array (size=0)
empty
protected 'hidden' =>
array (size=0)
empty
protected 'visible' =>
array (size=0)
empty
protected 'appends' =>
array (size=0)
empty
protected 'guarded' =>
array (size=1)
0 => string '*' (length=1)
protected 'dates' =>
array (size=0)
empty
protected 'touches' =>
array (size=0)
empty
protected 'observables' =>
array (size=0)
empty
protected 'with' =>
array (size=0)
empty
public 'exists' => boolean true
protected 'softDelete' => boolean false
I want it to output the image that is associated with that article(annonce), just like it is doing in my first view.
I guess you're getting 404 errors because if your image url is saved in the database as uploads/image.jpg
your browser will assume the image path is relative to the URL of the current page.
If you visit mylaravel.com/
it works fine but if you visit mylaravel.com/users/123/
it will spit out 404 because the browser requests the image from mylaravel.com/users/123/uploads/image.jpg
.
Laravel has undocumented helpers which will help devs solve issues like this.
<h2>{{ $annonce->title }}</h2>
{{ HTML::image($annonce->photo, '', array('class' => 'photo')); }}
The second image()
parameter is for image alt attribute, the third parameter array can be used for class, id, and data-* attributes.
EDIT: sorry, I didn't see citricsquid's answer as I didn't refresh the page before writing exactly the same answer as his.
Hey byjml,
The path to the image is correct according to console log. The path is:
http://localhost:8888/images/annoncebilleder/2014-04-25-09:51:07-pc3b8lsevogn-pc3a5-torvet-1.jpg But it still returns 404 not found.
Which is the exact same as the one that works in the other view. However I tried what you suggested with no luck :/
It works now! By using the image helper that you suggested. Thank you very much both of you guys!
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community