problem while trying to display image from database - php

I am trying to display a row from my database by id, the data displayed successfully but the image doesn't show (showing a broken link icon).
Here is the index.blade.php where the href to the row:
<a href="singlepost/{{$products->id}}">
<img src="storage/{{$templates->image_path2}}">
The route:
Route::get('/singlepost/{id}', 'App\http\controllers\TemplatesController#getPostById')->name('single.post');
The function getPostById
public function getPostById($id){
$products = DB::table('templates')->where('id', $id)->first();
return view('singlepost', compact('products'));
}
The singlepage.blade.php
<div class="row">
<div class="col-lg-4">
<img src="storage/{{$products->image_path}}">
</div>
<div class="col-lg-7">
<h2>{{$products->name}}</h2>
<p>{{$products->description}}</p>
</div>
</div>

When referencing publically accessible files, you want to do so from the /public/storage directory. Files from the /storage/app/public directory should be mapped from this directory to /public/storage using a symbloic link,
You create the symlink using:
php artisan storage:link
With that done, assets in /storage/app/public can now be accessed as if they existed in /public/storage. Use the asset helper to get your image.
<img src="{{ asset($templates->image_path2) }}" alt="Some alt tag ..." />
The above assumes a relative path of the asset from the /public/storage root, if you had the image in a subdirectory you would need to include that too.
For example, if you had your images in an img subdirectoy:
<img src="{{ asset('/img/' . $templates->image_path2) }}" alt="Some alt tag ..." />

Related

Image not showing in .blade file

I'm new to Laravel, but why the hell is this not working?
<img src='../img/netflix_logo.png' />
I've tried using php artisan storage:link and it said The [C:\xampp\htdocs\laravel-crud\public\storage] link has been connected to [C:\xampp\htdocs\laravel-crud\storage\app/public]. The links have been created. but the image is still not displaying in my .blade page..
If you image is in storage/img then
<img src="{{ url('storage/img/netflix_logo.png') }}" alt="" title="" />
If you image is in public/img folder then
<img src="{{ URL::to('/') }}/img/netflix_logo.png" alt="" title="" />
You probably need to use the "asset" helper from laravel/blade:
asset('/path/of/your/image')
It's the common way to call a static file from public folder (including storage if you already linked it)

Image path issues in my order list Laravel 8

I'm working on order module in Laravel 8.The issue I'm facing with my product image path.
Here is screen shot of my issue:
When I remove these brackets from image path it works OK:
Blade file code:
<h4>My Cart</h4>
#foreach($mem as $t)
<div class="top-cart-item">
<div class="top-cart-item-image">
<img src="{{URL::TO('')}}/storage/app/{{$t->proimage}}"> </div>
</div>
#endforeach
Change your image tag from
<img src="{{URL::TO('')}}/storage/app/{{$t->proimage}}">
to this
<img src="{{asset('/storage/app/$t->proimage')}}">

Laravel 7 not displaying the img

I have also used \ or
/ but not effective.
Blade
<img src="{{ public_path('images\about.jpg') }}">
PATH
-public
-images
-about.jpg
Using inspect element the link looks fine:
<img src="C:\xampp\htdocs\acc\public\images\about.jpg">
The asset() function is usually used to access the files in public directory of Laravel Application:
<img src="{{ asset('images/about.jpg') }}" alt="some description here">
The public_path() function returns the absolute path for a file which doesn't apply in this case.

Can't get path to image in HTML

Alright, I have a file named slider.php which contains a bootstrap slider and I'm inserting pictures inside the slider but I can't figure out the correct way of writing the path to the pictures
<div class="carousel-inner">
<div class="item active">
<img class="slide-image" src="" alt="">
</div>
<div class="item">
<img class="slide-image" src="" alt="">
</div>
<div class="item">
<img class="slide-image" src="" alt="">
</div>
The slider.php is in this location ecomcopy/resources/front/slider.php
and the pictures are in ecomcopy/resources/img/
I tried everything and I can't get the correct pictures path
Can someone help?
The way paths work is to go "up" to parent folders until you're at a common ancestor. Then go back down naming folders until you get to the file you're referencing.
ecomcopy/resources/front/slider.php
ecomcopy/resources/front/ (start)
ecomcopy/resources/ (up 1) ../
ecomcopy/resources/templates/ (down to folder) templates/
ecomcopy/resources/templates/img/ (down to folder) img/
ecomcopy/resources/templates/img/yourimage.png
(down to file) yourimage.png
result: ../templates/img/yourimage.png
should be ../../img/image_name.png
It would be much easier (at least on a live server setting) to just specify absolute paths for your URLs.
For example, the following HTML is all that is needed to serve your images:
<img src="/ecomcopy/resources/templates/img/yourimage.png" />
There is no real need to worry about complicated relative file paths.

How can I change the path of images/files inside different views?

This is working fine to show all the images inside the public folder of my home.blade view at www.mygallery.com
#foreach($items as $item)
<div class="box">
<div class="boxInner">
<img src="{{$item->image}}" alt="{{$item->title}}">
</div>
</div>
#endforeach
By the way this is how my routes file looks like.
Route::get('', array('as'=>'itemshome', 'uses'=>'ItemsController#show_items'));
Route::resource('upload', 'ItemsController');
Route::get('tags/{category?}', array('as'=>'itemstag', 'uses'=>'ItemsController#show_items'));
But when I try to filter results by category (www.mygallery.com/tags/cats) $item->image is trying to reach every image at tags/images/myimage.jpg which of course doesn't exist.
The thing is that I don't want to create a public/tags/images folder, so I wonder how can I explicitly point to the correct folder (public/images) no matter what view/route is making the call.
The problem here is that $item->image is a relative path. So depending on your current URL you will link to different paths.
To generate an absolute URL, use the asset() function:
<img src="{{ asset($item->image) }}" alt="{{$item->title}}">

Categories