Develop an E-Commerce Website With Laravel 5.4 - Part 6

Published Jul 02, 2017
Develop an E-Commerce Website With Laravel 5.4 - Part 6

All posts in this series:

In last post I disucssed about Db design and how Laravel facilitates interaction with Database. In this post I am going to integrate home page with Database.

If you notice there are two main section on home page, Hot Products and Top Products. First, I will create models for Hot Products and Top Products and then will connect the tables with the front view.

What are Models?

As I told earlier, Models provide abstraction by interacting with underlying database. Model classes are also used to create methods that can later be called within models or from a controller.
In order to create a Laravel model we will again use php artisan command:

php artisan make:model Models/HotProduct

and for Top Products

php artisan make:model Models/TopProduct

I created models in Models folder for code management but it is not necessary. You can also do like php artisan make:model TopProduct

Once it's done, index method in HomeController will look like:

public function index() {
    $hot_products = HotProduct::join('products','products.id','=','product_id')->get();
    $top_products = TopProduct::join('products','products.id','=','product_id')->get();
    return view('index')
      ->with('hot_products',$hot_products)
      ->with('top_products',$top_products);
    }
    

I joined two tables to get required info, get() here is for SELECTing the reccorrds. Once records are fetched, they are passed into view by passing variables in with function.

Now open views/index.blade.php and replace hard coded with @foreach directive

@foreach($top_products  as $top_product)
                    <div class="col-md-3 top_brand_left">
                        <div class="hover14 column">
                            <div class="agile_top_brand_left_grid">
                                <div class="tag"><img src="images/tag.png" alt=" " class="img-responsive" /></div>
                                <div class="agile_top_brand_left_grid1">
                                    <figure>
                                        <div class="snipcart-item block" >
                                            <div class="snipcart-thumb">
                                                <a href="single.html"><img width="150" height="150" title=" " alt=" " src="{!! $top_product->image !!}" /></a>
                                                <p>{!! $top_product->title !!}</p>
                                                <h4>${!! $top_product->discount_price !!} <span>${!! $top_product->original_price !!}</span></h4>
                                            </div>
                                            <div class="snipcart-details top_brand_home_details">
                                                <form action="#" method="post">
                                                    <fieldset>
                                                        <input type="hidden" name="cmd" value="_cart" />
                                                        <input type="hidden" name="add" value="1" />
                                                        <input type="hidden" name="business" value=" " />
                                                        <input type="hidden" name="item_name" value="Fortune Sunflower Oil" />
                                                        <input type="hidden" name="amount" value="7.99" />
                                                        <input type="hidden" name="discount_amount" value="1.00" />
                                                        <input type="hidden" name="currency_code" value="USD" />
                                                        <input type="hidden" name="return" value=" " />
                                                        <input type="hidden" name="cancel_return" value=" " />
                                                        <input type="submit" name="submit" value="Add to cart" class="button" />
                                                    </fieldset>
                                                </form>
                                            </div>
                                        </div>
                                    </figure>
                                </div>
                            </div>
                        </div>
                    </div>
                @endforeach

{!! !!} is used to display values on page, in each iteration fields are being accessed and displayed.

That's it for now. In coming post will discuss the detail page and testing of home page with further details. Stay tuned!

Like always, updated code is available on Github

Discover and read more posts from Adnan Siddiqi
get started