Displaying image from database in grid - laravel - php

I have my image path in my database and now, i am trying to get the image into my html grid. But when i try to fetch the image, i get an error
Property [path] does not exist on this collection instance.
But then, there is path attribute in my table in the database. Has it got to be an error from my models? My model seems right to me. Anyway what am i not doing right here, please?
Thanks for your concern
HTML
<figure>
<div class="snipcart-item block">
<div class="snipcart-thumb">
<img src="{{$product->images->path}}" alt=" " class="img-responsive" />
<p>{{$product->name}}</p>
<h4>GH₵ {{ number_format($product->price,2) }} </h4>
</div>
<div class="snipcart-details top_brand_home_details">
<form action="#" method="post">
<fieldset>
</fieldset>
</form>
</div>
</div>
</figure>
Image
public function products()
{
return $this->belongsTo(Product::class);
}
Product_images
public function images()
{
return $this->hasMany(Image::class);
}

You need to make a foreach as it's give you a collection.
Hence, try to display it like:
#foreach($product->images as $image)
#if($image->path)
<img src="{{ $image->path }}" alt=" " class="img-responsive" />
#else
<p>No image to display!</p>
#endif
#endforeach
Hope this will helps you!

This happens because the Product model hasMany Images.
In order to reach the path property you should do this:
#foreach($product->images as $image)
{{$image->path}}
#endforeach
Or do this in your Product Model and change your database structure
public function image()
{
return $this->hasOne(Image::class);
}
I hope it helps.

If your path does not any issue, the following is the correct way to display an image in Laravel blade template.
<img src="{{URL::asset('directory/name_of_image_file.file_format')}}">

You don't have field path in DB images
You code must be:
<img src="{{$product->images->[you field in Db images]}}" alt=" " class="img-responsive" />

HTML
#foreach($products as $product)
<figure>
<div class="snipcart-item block">
<div class="snipcart-thumb">
<img src="{{$product->images->first()->path}}" alt=" " class="img-responsive" />
<p>{{$product->name}}</p>
<h4>GH₵ {{ number_format($product->price,2) }} </h4>
</div>
<div class="snipcart-details top_brand_home_details">
<form action="#" method="post">
<fieldset>
</fieldset>
</form>
</div>
</div>
</figure>
#endforeach

<a href="{{ route('product.view', $product->slug)}}">
<img src="{{$product->images->first()->path}}" alt=" " class="img-responsive" />
</a>
$product->images is a collection, you need to get the image out of the collection. for now I have used the first() which will fetch the first image, you may want to loop through the collection depending on your scenario. You may want to read about an answer have written here

Related

img of admin auth doesn't loading in laravel 8

i am trying to show the image of the admin user but no thing show .
. and name email of the user showing correctly
#php
$adminData = Auth::user();
#endphp
< div class="widget-user-header bg-black" >
<h3 class="widget-user-username">Admin Name : {{ $adminData->name }}</h3>
Edit Prfile
<h6 class="widget-user-desc">Email : {{ $adminData->email }}</h6>
</div>
<div class="widget-user-image">
<img class="rounded-circle" src="{{(!empty(asset($adminData->profile_photo_path) ))? url('upload/admin_images/'.$adminData->profile_photo_path):url('upload/no_image.jpg')}}">
</div>
You have to use like below
#if($adminData->profile_photo_path)
<img class="rounded-circle" src="{{url('/'.$adminData->profile_photo_path)}}">
#else
<img class="rounded-circle" src="{{url('/no_image.jpg')}}">
#endif
You Can check if admin photo contains null value or not
<img class="rounded-circle" src="{{$adminData->profile_photo_path != null? url('upload/admin_images/'.$adminData->profile_photo_path):url('upload/no_image.jpg')}}">
Let's do it like this. Make sure you have a default image like no_image.jpg as a fallback function. It does display the image in case if the profile_photo_path attr exists or falls into no_image if not. However, I think your issue is because you don't point to storage folder.
<img src="{{ storage_path($adminData->profile_photo_path ?? 'admin_images/no_image.jpg')) }}" />

laravel: looping images folder with index value

i am trying to show an image from the images folder however the image is not showing although i am able to see it in an array if i die and dump the "$image". The images are located in "public/images" folder
blade file
<div class="gallery">
#foreach($posts as $post)
<div class="round-post">
#foreach ($post->images as $image)
........
#endforeach
#endforeach
</div>[![enter image description here][1]][1]
when i <img src="/images/ <?php dd($image[0]); i get
this bunch of code get your images that exist in your images folder:
public function index(){
$images = \File::allFiles(public_path('images'));
return View('pages.form')->with(array('images'=>$images));
}
this will show the images in your blade.
<ul>
#foreach ($images as $image)
<li style="width:80px;display:inline-block;margin:5px 0px">
<input type="checkbox" name="images[]" value="{{$image->getFilename()}}"/>
<img src="{{ asset('images/' . $image->getFilename()) }}" width="50" height="50">
</li>
#endforeach
</ul>
for more information you can read this article.
Good luck my friend!

Taking values from a certain number Laravel

I have a database with images related to one product, and I'd like to take those images there only from the second one.
The code to my view is this. I put a line for each image
#foreach($allImages as $image)
<img src="{{ asset('merchants/images/products/' . $image->image) }}" alt="little picture">
#endforeach
I hope I'm clear enough on what I'd like to do...
#foreach($allImages->skip(1) as $image)
<img src="{{ asset('merchants/images/products/' . $image->image) }}" alt="little picture">
#endforeach
This will skip the first record
You can do something like this to skip the first image in the array
#foreach($allImages as $image)
#if($loop->first){
#continue;
}
else{
<img src="{{ asset('merchants/images/products/' . $image->image) }}" alt="little picture">
}
#endforeach
I hope it is helpful for you

loading images in laravel PHP

Strange stuff going on here. For some reason the image loads correctly on one page, however the exact code im using does not work on the other page.
Code that works: index.blade.php
#if(count($posts) > 1)
#foreach($posts as $post)
<div class= "well">
<h2>{{$post->title}}</h2>
<small>{{$post->body}}</small>
<br>
<img src="{{$post->image}}" alt="profile Pic" height="200" width="200">
</div>
<br>
<br>
#endforeach
Code that does not work: Show.blade.php
<h1>{{$post->title}} </h1>
<br>
<h2>{{$post->body}}</h2>
<br>
<img src="{{$post->image}}" alt="profile Pic" height="250" width="250">
<div class= 'Info'>
<br>
PostsController.php
public function show($id)
{
$post = Post::find($id);
$review = Review::all()->where('post_id', $id);
return view('posts.show', compact('post', 'review'));
}
Im having issues with the img src part on show.blade.php. Everything else such as title and body is showing but the image on that page is not. Does anyone have this issue ?
You may use the url method to get the URL for the given file.
<img src="{{ Storage::url($post->image) }}" alt="profile Pic" height="250" width="250">
Also, to make public disk accessible from the web, you should create a symbolic link from public/storage to storage/app/public.
To create the symbolic link, you may use the storage:link Artisan command:
php artisan storage:link
The most common mistake in this problem is that do not pay attention to relative path.
Use absolute path for it.And you can use Inspect element of browser for compare 2 pages data.

How to get an image on the desired page by clicking on that image using Laravel?

I am working on the laravel 5.0. I am getting an error: "trying to get property of non-object".I want to get an image on the desired page by clicking on that particular image. There are so many images on the page, i just want after clicking on any image, it will be appear on the desired page.
here is my code:
My Routes:
Route::get('/image/big/{id}' ,[
'uses'=>'GalleryController#bigimage',
'as'=>'bigimage'
]);
My controller:
public function bigimage($id){
$images=image::findorFail($id);
return view('bigimage' , ['images'=>$images]);
}
My view from where i am calling the required Route:
#foreach($gallery->images as $image)
<li>
<a href="{{ URL('/image/big/'.$image->id) }}">
<img id="jumboimage2" src="{{ url($image->file_path) }}"></a>
</li>
<li>
<a href="{{ URL('/image/delete/'.$image->id) }}" id="margin">
<span id="margin" class="glyphicon glyphicon-remove-sign"></span></a>
</li>
#endforeach
My view where i want to get the required image:
<div class="row">
<section class="col-md-3">
#foreach($images as $image)
{{$image->id}}
#endforeach
</section>
</div>
try this one out
$image = image::findOrFail($id);
return view('bigimage' , ['image' => $image])
$images=image::findorFail($id); should be $images=image::findOrFail($id);.
findOrFail() returns a single model, i.e. one image, so I would have:
$image = image::findOrFail($id);
return view('bigimage' , ['image' => $image]);
You don't show where image:: comes from, however usually this is from a facade so is Image:: with a capital.
The view would then not have the #foreach loop as you'd only have a single image.
<div class="row">
<section class="col-md-3">
{{$image->id}}
</section>
</div>
You also need to elaborate on this to get from the id to an image but I'm assuming that's just not shown.
If you were able to give some detail about which line is causing the error this would make it easier to diagnose, as indicated by Maraboc there could be an issue with $gallery as well.

Categories