Shawdow's avatar

How can i make the products image as link and show the list of products images

Hi,

this is an ecommerce site where the users click on the particular product images show the list of images in next page.

i have written below code to make the image as link

<div class=row">
   <div class="col-xs-3">
@foreach($product as $products)
    <div class="images" >
<a href="" style="text-decoration:none">
        <img src="images/{{ $products->image_3 }}" class="img-responsive">
        <center>
 {{ $products->name }}<br>
            {{ $products->discount }}% Off<br>
            <i class="fa fa-inr"></i> {{ $products->price }}<br>
        </center>
    </div>
</a>
  @endforeach 
</div>
</div>

how can i write the route and controller to show the particular product images??

Thanks,

0 likes
3 replies
bobbybouwmann's avatar

Well you just need to add a new route that looks like this maybe

Route::get('product/{product}/images', 'ProductsController@images');

From there you only have to return the correct view with the correct data

// ProductsController.php

public function images(Product $product)
{
    return view('products.images', compact('product'));
}

If you have no clue what above code does I suggest to first look at some free series here on Laracasts and read the documentation. It's all explained :)

arthurvillar's avatar
Level 9

It depends on how you have your database and models structured. But it will be something like this.

  • On the view file
<a href="/products/{{$product->id}}" style="text-decoration:none">
  • On the routes file
Route::get('/products/{product}', 'ProductsController@show');
  • On the the ProductsController
public function show(Product $product)
{
    // Laravel will fetch the correct product and return it in the variable $product
    return view('product-page-here', compact('product'));
}

I am assuming you have a controller named ProductsController and a model named Product

Shawdow's avatar

Thank you guys for your replies one thing how to make it both the products name and products id in the route file

example

/lenovo/7 where the lenovo is the product name 7 is the product id any ideas??

1 like

Please or to participate in this conversation.