Attempt to read property "image" on bool in Laravel - php

I am new to Laravel Plz help me to solve this problem.
I have multiple images of a single product in the Database and want to show them in a carousel in foreach loop.
i also have the product id and images in a database table.
My Function
public function view_product_details($id)
{
$product_details_images = product_attr_images::where('product_id', $id)->first();
// dd($product_details_images);
return view('frontend.view_product_details', compact( 'product_details_images'));
}
My View File
<div class="carousel-inner border">
#foreach ($product_details_images as $key=> $images)
<div class="carousel-item{{$key==0 ? 'active' : ''}} ">
<img class="w-100 h-100" src="product/{{$images->image}}" alt="Image">
</div>
#endforeach
</div>
I want to fetch all images and show them in the carousel in foreach loop but I can't. i am facing error of images in bool. I don't know what is happening.
plz help me. I will be very thankful to you

Please follow Laravel's naming conventions. They are not only easier for us to read, but also used by Laravel to perform to many magical operations under the hood.
You see one image because you used first() method which returns the first matched instance. In order to fetch all matched instances in a collection, use get().
product_attr_images::where('product_id', $id)->get();
I didn't understand your desing. Your page will only have the images, and display them in carousel, right? If this is the case, your blade file should work, but you should add a space between carousel-item and {{$key==0. If this isn't the case, you should pass the images as a part of a bigger data set from controller to the product page.

Related

Laravel 5.8: Loading images not working properly

I'm working with Laravel 5.8 and in this project I want to show some images that I had uploaded through database like this:
And at the view, I tried this:
<div class="text-center" style="background-image: url('public\images\tavana\{{ $images[0]->path }}');height: 220px;">
But the problem is the image does not load!
However the image already exists at the this directory:
public\images\tavana\imagename.jpg
And other data such as title, subtitle properly shows up.
So how to properly show the image?
use asset to generate url
<div class="text-center" style="background-image: url('{{asset('images/tavana/'.$images[0]->path)}}');height: 220px;background-repeat: no-repeat">
also not sure why you are accessing 0th index $images[0]->path instead you can return first() so it will be $images->path
If you have multiple then use loop
You can't try:
<div class="text-center" style="background-image: url({{url('/')}}.'public/images/tavana/{{ $images[0]->path }}');height: 220px;background-repeat: no-repeat">

Query data in database to show posts

How to foreach 5 posts orderBy DESC show 2 type css, 5 posts from the same category
okay maybe this question not clear but i try to figure out what you want.you can filter what content will show by just add some condition in your loop
#foreach($news as $key => $new)
#if($key < 2)
//div with big image class
#else
//div with small image class
#endif
#endforeach
Laravel Blade allows to use a helpful set of variable like
Loop Variable
When looping, a $loop variable will be available inside of your loop. This variable provides access to some useful bits of information such as the current loop index and whether this is the first or last iteration through the loop:
#foreach($news as $new)
#if($loop->index < 2)
//div with big image class
#else
//div with small image class
#endif
#endforeach

If loops with DB Queries in Laravel Blade Templating

I'm trying to create a continous system for an online magazine's website that I'm building. I want to obviously display artwork differently than I do with other pieces (say a poem or an essay), but I'm having trouble composing the loop that works with both of them. Here's the situation:
I have a $current_genre variable that tells me which genre is currently being read (this is all done via an admin panel). As below, the loop is supposed to identify what the current genre is and then display a certain "tile" accordingly:
#foreach ($pieces as $piece)
#if ($current_genre == 'Artwork')
<div>
<p>Display for images</p>
</div>
#else
<div>
<p>Display for other items</p>
</div>
#endif
#endforeach
I'm not sure what the problem is, as this looks fine to me. It certainly isn't the controller, because I echo the $current_genre variable in other places throughout the page, so I'm not sure what's going on.
EDIT for clarification: The main problem is that no matter what I do, the artwork always appears as a regular piece should, and never as I had it set under the #if($current_genre == 'Artwork') section
Thanks in advance for any help.
Assuming genre is the property of $peice, you can implement the loop as:
#foreach($pieces as $piece)
{
#if($piece->genre == 'Artwork')
** Some Code **
#else
** Some Other Code **
#endif
}
#endforeach
If this does not answer your question,
you could make the question clearer by showing how you are getting the $current_genre variable.
$current_genre is not defined how can you compare like that #if ($current_genre == 'Artwork') you have to access the #if ($piece->current_genre == 'Artwork') like that. hope this will help you

How to retrieve image from database and display it on view using blade in laravel

I'm using
php artisan storage:link to store the images to database. As expected Images are storing in database. But I've problem in retrieving the images and displaying them on the view.I tried different type of methods but not working. This is my code to get the records.
public function show($id)
{
$Attendees=Attendee::where('event_id',$id)->OrderBy('first_name','ASC')->get();
return view('Attendee',['Attendees'=>$Attendees,'id'=>$id]);
}
This is my code to display data on view.
<div class="col-sm-10" style="margin:0px 0px 0px 225px">
Add Attendee
#foreach($Attendees as $deligate)
<img src="{{ asset('images/$deligate->id') }}">
<h4><span>{{$deligate->first_name}}</span> <span>{{$deligate->last_name}}</span></h4>
#endforeach
</div>
can anyone help me on this please???
If you sure that your image is stored into the Database. so you have to retrieve the BLOB data.
$blob = $deligate->image_field; // image field name of Table
{{'<img src="data:image/jpeg;base64,'.base64_encode( $blob ).'"/>' }}
After spending lot of time on it, I got the answer.
<img src="{{ URL::asset('storage/images'.$deligate->profile_image) }}">

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