Show list of all categories
In the ShopController
, get all categories by calling Category->findAll()
method.
app/Http/Controllers/ShopController.php
<?php
$categories = Category::findAll()
Now, the model contains a list all the categories in the camera store, this list encapsulated in Page
object.
Page
object is a container which wrap a collection of objects,
it extends ArrayObject
so you can iterate over its content using the standard for
loop, and can be easily used within jinja2 templates.
We will use this collection of Category
objects to populate the top navigation menu of the store.
resources/views/layouts/default.blade.php
<ul class="nav navbar-nav" id="category_menu">
<li><a href="/products" >All</a></li>
@foreach($categories as $cat)
@if ($cat->name != 'Uncategorized' )
<li><a href="/categories/{{ $cat->id }}" >{{ $cat->title }}</a></li>
@endif
@endforeach
<li><a href="#" id="show_search_box"><span class="glyphicon glyphicon-search"></span></a></li>
</ul>
Display list of all brands
Using the same technique, We can get a list of the available brands, and display them in the sidebar navigation. for example in the details action method:
app/Http/Controllers/ShopController.php
<?php
$brands = Brand::findAll()
Then display it in the sidebar
resources/views/layouts/products.blade.php
<div class="product-list">
<h2>Browse By Brand</h2>
<ul class="product-list">
@foreach($brands as $brand)
<li><a href="/brands/{{ $brand->id }}">{{ $brand->title }}</a></li>
@endforeach
</ul>
</div>
Browse products
Likewise, We can obtain and show a list of all products.
app/Http/Controllers/ShopController.php
<?php
public function products(Request $request){
$products = Product::findAll();
return view('shop/products', array(
'cart' => ShoppingCart::get(),
'products' => $products,
'brands' => Brand::findAll(),
'categories' => Category::findAll(),
'featured' => Collection::findOne(['name' => 'featured']),
));
}
To show the products, iterate over the product collection
resources/views/shop/products.blade.php
<ul>
@foreach($products as $p)
<li>
<div>
<a class="cbp-vm-image" href="/products/{{ $p->id }}">
<div class="simpleCart_shelfItem">
<div class="view view-first">
<div class="inner_content clearfix">
<div class="product_image">
<img src="{{ $p->mainPhoto->url }}"
class="img-responsive" alt=""/>
<div class="mask">
<div class="info">Quick View</div>
</div>
<div class="product_container">
<div class="cart-left">
<p class="title">{{ $p->title }}</p>
</div>
<div class="pricey"><span class="item_price" >$ {{ $p->price }}</span></div>
<div class="clearfix"></div>
</div>
</div>
</div>
</div>
</div>
</a>
<div class="cbp-vm-details">
{{ $p->shortDescription }}
</div>
<a class="cbp-vm-icon cbp-vm-add item_add add_to_cart_button"
data-product_id="{{ $p->id}}" href="#">Add to cart</a>
</div>
</li>
@endforeach
</ul>
This may looks very boring and repetitive, but we want to make a point out of that: the Java SDK has a very consistent, once you learned a small subset you ‘ll find the rest of it very familiar and predectable.
Searching
Searching for a certain product(s) require a very little code change. Just pass the property you want to search with to the find_all
method.
app/Http/Controllers/ShopController.php
<?php
public function products(Request $request){
if($request->has('query')){
$products = Product::findAll(['title' => $request->query('query')]);
}else{
$products = Product::findAll();
}
return view('shop/products', array(
'cart' => ShoppingCart::get(),
'products' => $products,
'brands' => Brand::findAll(),
'categories' => Category::findAll(),
'featured' => Collection::findOne(['name' => 'featured']),
));
}
Filter products by category
Now, Let’s see how to filter this list of products by category
Because filtering products by category and brand are frequently used operations,
there is a dedicated methods for these operations: find_all_by_category
and find_all_by_brand
respectively.
All findAll*
methods return Page
object wrapping the result set, so it is possible to use the same view template to display the search results.
app/Http/Controllers/ShopController.php
<?php
public function categories($id){
return view('shop/products', array(
'cart' => ShoppingCart::get(),
'products' => Product::findAll(['category' => $id]),
'brands' => Brand::findAll(),
'categories' => Category::findAll(),
'featured' => Collection::findOne(['name' => 'featured']),
));
}
Filter products by brand
Likewise, You can filter products by brand, just use the Product#find_all_by_brand
instead.
app/Http/Controllers/ShopController.php
<?php
public function brands($id){
return view('shop/products', array(
'cart' => ShoppingCart::get(),
'products' => Product::findAll(['brand' => $id]),
'brands' => Brand::findAll(),
'categories' => Category::findAll(),
'featured' => Collection::findOne(['name' => 'featured']),
));
}
Viewing product details
You can get single product by its ID
, just use Product#find_by_id
, the code is straightforward:
app/Http/Controllers/ShopController.php
<?php
public function product_details($id){
return view('shop/single', array(
'cart' => ShoppingCart::get(),
'product' => Product::findById($id),
'brands' => Brand::findAll(),
'categories' => Category::findAll(),
'featured' => Collection::findOne(['name' => 'featured']),
));
}
And this is the view template:
resources/views/shop/single.blade.php
<div class="new-product">
<div class="col-md-5 zoom-grid">
<div class="flexslider">
<ul class="slides">
@foreach( $product->photos as $ph)
<li data-thumb="{{ $ph->url}}">
<div class="thumb-image"> <img src="{{ $ph->url }}"
data-imagezoom="true" class="img-responsive" alt="" /> </div>
</li>
@endforeach
</ul>
</div>
</div>
<div class="col-md-7 dress-info">
<div class="dress-name">
<h3>{{ $product->title }}</h3>
<span>$ {{ $product->price }}</span>
<div class="clearfix"></div>
<p>{{ $product->description }}</p>
</div>
<div class="purchase">
<a class="cbp-vm-icon cbp-vm-add item_add add_to_cart_button" href="#"
data-product_id="{{product.id}}">Add To Cart</a>
</div>
</div>
</div>
Getting Collections
For the sake of completeness, we demonstrate how to get and display Collection
s,
Also this is a good opportunity to show you how you can use Collection
s in your own projects.
app/Http/Controllers/ShopController.php
<?php
public function index(Request $request){
return view('shop/index', array(
'cart' => ShoppingCart::get(),
'categories' => Category::findAll(),
'collections' => Collection::findAll(),
));
}
In the home-page of the store, we use Collection
s to show customers selected collections of our products.
We use collection for the available deals, another for the featured products, and one for the newly added products.
resources/views/shop/index.blade.php
@foreach($collections as $coll)
<div class="container collections" >
<h3 class="like text-center">{{ $coll->title }}</h3>
<ul id="{{ $coll->name }}" class="flexiselDemo3">
@foreach($coll->products as $p)
<li>
<a href="/products/{{ $p->id }}">
<img src="{{$p->mainPhoto->url}}" class="img-responsive" alt="" />
</a>
<div class="product liked-product simpleCart_shelfItem">
<a class="like_name" href="/products/{{ $p->id }}">{{ $p->title }}</a>
<p><a class="item_add add_to_cart_button"
data-product_id="{{$p->id}}"><i></i>
<span class=" item_price">$ {{ $p->price }}</span>
</a></p>
</div>
</li>
@endforeach
</ul>
</div>
@endforeach
You can get single collection by name, use Collection::findByName()
app/Http/Controllers/ShopController.php
<?php
public function product_details($id){
return view('shop/single', array(
'cart' => ShoppingCart::get(),
'product' => Product::findById($id),
'brands' => Brand::findAll(),
'categories' => Category::findAll(),
'featured' => Collection::findOne(['name' => 'featured']),
));
}
We use this in the product details shop/single.html
to show featured products.
resources/views/layouts/products.blade.php
<h3 class="like text-center">{{ $featured->title }}</h3>
<ul id="flexiselDemo3">
@foreach($featured->products as $p)
<li>
<a href="/products/{{ $p->id }}">
<img src="{{ $p->mainPhoto->url}}" style="width: 220px;" class="img-responsive"/>
</a>
<div class="product liked-product simpleCart_shelfItem">
<a class="like_name" href="/products/{{ $p->id }}">{{ $p->title }}</a>
<p><a class="item_add add_to_cart_button" href="#"
data-product_id="{{ $p->id}}"><i></i>
<span class=" item_price">$ {{ $p->price }}</span>
</a></p>
</div>
</li>
@endforeach
</ul>