im developing a blog where all post can have more than one categories, and after I want those categories to be the class attribute from a div because im using data-filters to show those post that you select from the categories menu and hide those who are not part of the category.
Im using the next code but if you check {{$post->categorias[0]->nombre}} (where categorias is the category section to iterate and "nombre" is the name of the categories) only will show the first category and I want to show all the categories that belongs to that post, not only the first one.
#foreach($posts as $post)
<figure class="{{$post->categorias[0]->nombre}}">
<a href="project.html" class="thumb">
#foreach($post->imagenes as $imagen)
<img src="/imagenes/articulos/{{$imagen->url}}" alt="alt"/>
#endforeach
</a>
<figcaption>
{{$post->nombre}}</h3>
{{$post->contenido}}</figcaption>
</figure>
#endforeach
I've already tried to iterate inside class=" - " but didn't work, something like this:
<figure class="#foreach ($post->categorias as $po)
{{$po->nombre}}
#endforeach">
bud didn't work
I will appreciate any help. Thank you
BLADE
#foreach($posts as $key => $post)
<figure class="{{$post->categorias[$key]->nombre}}">
<a href="project.html" class="thumb">
#foreach($post->imagenes as $imagen)
<img src="/imagenes/articulos/{{$imagen->url}}" alt="alt"/>
#endforeach
</a>
<figcaption>
{{$post->nombre}}</h3>
{{$post->contenido}}</figcaption>
</figure>
#endforeach
The $key is the 'counter' to get the right data.
#foreach($posts as $post)
<figure class="#foreach($post->categorias as $key => $po)
{{$post->categorias[$key]->nombre}}#endforeach">
<a href="project.html" class="thumb">
#foreach($post->imagenes as $imagen)
<img src="/imagenes/articulos/{{$imagen->url}}" alt="alt"/>
#endforeach
</a>
<figcaption>
{{$post->nombre}}</h3>
{{$post->contenido}}</figcaption>
</figure>
#endforeach
Thanks to #djl for the help :)
Related
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!
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
I have been learning "code" just over a year for a project i have and have got stuck on a particular function that i am trying to create for the user. I have lots of divs with different information being rendered from a database. Html and css looks great I have then created blade #sections to hold the info but struggling to add functionality.
#foreach($canal as $canal)
#section('water-left')
<img class="img-responsive" src="{{ asset('images/canals/' . $canal->image)}}">
#section('location')
<strong class="words myh4">{{$canal->name}}</strong>
#section('water-right')
<img class="img-responsive" src="{{ asset('images/canals/' . $canal->image)}}">
#endsection
#endsection
#endsection
#endforeach
I have paginated contents of database to 3 and am trying to attatch the id1 image to ('water-left'), the id2 name to ('location') and id3 image to('water-right') currently i get just id1 in all sections any help please my controller looks like this
public function getcanalimage()
{
$canal = Canal::paginate(3);
return view('waters.canal_fishing',compact('canal'));
}
any help would be much appreciated thanks.
Try to use foreach loop, because $canal is array with more one values and you need use a loop to show all them:
#section('water-right')
#foreach($canal as $item)
<img class="img-responsive" src="{{ asset('images/canals/' . $item->image)}}">
#endforeach
#endsection
It has been a while since the question was posted but, for anyone using Laravel 5.8, there seems to be an undocumented blade tag/directive called #overwrite.
So replacing #endsection with #overwrite should do the work.
#foreach($canal as $item)
#section('water-left')
<img class="img-responsive" src="{{ asset('images/canals/' . $canal->image)}}">
#section('location')
<strong class="words myh4">{{$item->name}}</strong>
#section('water-right')
<img class="img-responsive" src="{{ asset('images/canals/' . $item->image)}}">
#overwrite
#overwrite
#overwrite
#endforeach
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.
I am trying to create a dynamic image link (in Laravel 5.0), like this:
#foreach($result as $r)
<div>
<a href="{{URL::to('productdetail?id=<?php echo $r->id; ?>')}}">
<img src="..\public\uploads\{!! $r->fileName !!}" height="200" width="250"/>
</a>
</div>
#endforeach
and the link it generates is:
http://localhost:8000/{{URL::to('productdetail?id=1'}}
But I want to generate a link like this:
http://localhost:8000/productDetail?id=1
Any help?
Tux almost had it, but you need to put the variable outside the string. This should work:
<a href="{{ URL::to('productdetail?id='.$r->id) }}">
But why are you using a GET variable for this? The beauty of Laravel is that you can have the ID as part of the URL. If I was writing this I would have this in my routes file:
Route::get('/product/{stub}/{id}', ['as' => 'prodDetailPage', 'uses' => 'ProductDetail#index');
Then you could just use <a href="{{ route('prodDetailPage', [$r->stub, $r->id]) }}">
For stub you can create a function to turn the title into a stub, it would need to convert it to lower case, remove special chars and convert spaces to hyphens.
You can directly use:
#foreach($result as $r)
<div>
<a href="{{URL::to('productdetail?id=$r->id')}}">
<img src="..\public\uploads\{!! $r->fileName !!}" height="200" width="250"/>
</a>
</div>
#endforeach
Well, I have got this, actually I was putting the PHP tags at a wrong place, so the final link would be like this:
<div>
<a href="{!! URL::to('productdetail?id=') !!}<?php echo $r->id; ?>">
<img src="..\public\uploads\{!! $r->fileName !!}" height="200" width="250"/>
</a>
</div>