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

Published Aug 28, 2017Last updated Feb 24, 2018
Develop an E-Commerce Website With Laravel 5.4 - Part 7

All posts in this series:

Sorry for another big gap. Hopefully would be able to finish up now 😃

In last post we learnt how to integrate backend with views. Now it's time to move forward.

In this post we are going to work on individual product page. For that we will do following things:

  • Crete ProductController controller.
  • Create the route for the single page.
  • Integrate the product model to show data.

Controller creation is simpler, all I have to do is to run the following command in Project folder:

php artisan make:controller ProductController --resource

I added --resource parameter in the command which makes it a Resourceful Controller. It is a special type of controller which contains all methods can be performed with a model like GET,POST,DELETE,PUT etc. When you run the command it will create the controller with methods like index(), create() etc.

One can create entire website within a single folder, HomeController in our case but that is not a good practise. Each Entity should have a separate folder.

OK, controller is created, now I am going to create route. Since the controller is a Resourceful Controller we will not need to create separate routes for each method. Instead I am going to do the following in routes/web.php file:

Route::resource('product', 'ProductController')

Now, if I check the list of all routes by running php artisan route:list, something like below will appear:

| Domain | Method    | URI                           | Name            | Action                                              | Middleware   |
|        | GET|HEAD  | /                             |                 | App\Http\Controllers\HomeController@index           | web          |
|        | GET|HEAD  | _dusk/login/{userId}/{guard?} |                 | Laravel\Dusk\Http\Controllers\UserController@login  | web          |
|        | GET|HEAD  | _dusk/logout/{guard?}         |                 | Laravel\Dusk\Http\Controllers\UserController@logout | web          |
|        | GET|HEAD  | _dusk/user/{guard?}           |                 | Laravel\Dusk\Http\Controllers\UserController@user   | web          |
|        | GET|HEAD  | api/user                      |                 | Closure                                             | api,auth:api |
|        | GET|HEAD  | product                       | product.index   | App\Http\Controllers\ProductController@index        | web          |
|        | POST      | product                       |   | App\Http\Controllers\ProductController@store        | web          |
|        | GET|HEAD  | product/create                | product.create  | App\Http\Controllers\ProductController@create       | web          |
|        | GET|HEAD  | product/{product}             |    | App\Http\Controllers\ProductController@show         | web          |
|        | PUT|PATCH | product/{product}             | product.update  | App\Http\Controllers\ProductController@update       | web          |
|        | DELETE    | product/{product}             | product.destroy | App\Http\Controllers\ProductController@destroy      | web          |
|        | GET|HEAD  | product/{product}/edit        | product.edit    | App\Http\Controllers\ProductController@edit         | web          |

Laravel itself took care of the necessary routes and created named routes. For instance product.create can be referred when we are going to add a product by calling create method of ProductController.

Since our goal is to display a single product, we are going to use show() method. For that purpose first I will create a view file. Create a new blade template file (.blade.php) file in views/ folder. For sake of management I created a new folder, named product and added a new file, named show.php in it. In the file I added the following:

    <div class="row margin-top">
        <div class="col-md-11 col-md-offset-1">
            A Single Product

And in show() method of ProductController I made following changes:

public function show( $id ) {
    return view('product/show');

I then accessed the page by hitting the URL, where 21 is the product Id. It shows follownig:

Screen Shot 2017-08-28 at 1.24.27 PM.png

Ok So far so good. I then went to single product page of the theme and copied the required HTML and pasted in my view and now it looks like this:

Screen Shot 2017-08-28 at 1.28.11 PM.png

We have to replace the static text with the text coming from the table. For that we will have to access data via model. Since there is no Productmodel yet so let's create one!

php artisan make:model Models/Product

And then go back to ProductController and call a single product by Id

public function show( $id ) {
   	$product = Product::find( $id );

   	return view( 'product/show' )
   		->with( 'product', $product );

It will fetch the product by Id $id and then will pass the variable in the view by calling with method.

Now data is passed to the view, it's time to access and make it view on the page.

In show.blade.php file, I will replace static text with code like:

<h5>{!! $product->title !!}</h5>
 <div class="col-md-4 agileinfo_single_left">
   <img id="example" src="{!! $product->image !!}" alt=" " class="img-responsive">

$product is the variable passed in with method,the fields like $title,image are the table fields passed by the model in the controller. If all works well then page will now look like below:

Screen Shot 2017-08-28 at 1.46.12 PM.png

Before I conclude, let's integraste the single page on main page. Go to index.blade.php and change single.html to {!! route('',$hot_product->id) !!}

The route routine takes the route name as first parameter, in our case and parameter, in our case $id. The final URL will look like this:

That's it for now. In coming post we will add the product in cart. Till then..Bye!!

The updated code has been push to Github.

Discover and read more posts from Adnan Siddiqi
get started