Skip to content
This repository was archived by the owner on Feb 8, 2024. It is now read-only.

5.1. Getting started

Shinichi Okada edited this page May 2, 2015 · 1 revision

Setting up

cd Sites
composer create-project larvel/laravel ecomm

// storage 
cd ecomm
sudo chmod -R 755 storage

database

Open .env and change accordingly. We are using mysql for this project

Copy css img and js dir to public dir

Copy starting-resources/product.html to app/resources/views/layouts/main.blade.php.

Change

<link rel="stylesheet" href="css/normalize.css">
<link rel="stylesheet" href="css/main.css">
<script src="js/vendor/modernizr-2.6.2.min.js"></script>

to

{{ HTML::style('css/normalize.css') }}
{{ HTML::style('css/main.css') }}
{{ HTML::script('js/vendor/modernizr-2.6.2.min.js') }}

Change

<img src="img/down-arrow.gif" alt="Shop by Category" />
// and     
<img src="img/user-icon.gif" alt="Sign In" />Sign In <img src="img/down-arrow.gif" alt="Sign In" />
// and
<img src="img/user-icon.gif" alt="Andrew Perkins" /> Andrew Perkins <img src="img/down-arrow.gif" alt="Andrew Perkins" />
// and
<img src="img/blue-cart.gif" alt="View Cart">
// and
<img src="img/payment-methods.gif" alt="Supported Payment Methods">
// and
<script>window.jQuery || document.write('<script src="js/vendor/jquery-1.9.1.min.js"><\/script>')</script>
<script src="js/plugins.js"></script>
<script src="js/main.js"></script>

to

{{ HTML::image('img/down-arrow.gif', 'Shop by Category')  }}
// and     
{{ HTML::image('img/user-icon.gif','Sign In')  }} Sign In {{ HTML::image('img/down-arrow.gif','Sign In') }}
// and 
{{ HTML::image('img/user-icon.gif','Andrew Persins') }} Andrew Perkins {{ HTML::image('img/down-arrow.gif','Andrew Perkins')}}
// and
{{ HTML::image('blue-cart.gif','View Cart') }}
// and
{{  HTML::image('img/payment-methods.gif', 'Supported Payment Methods') }} 
// and
<script>window.jQuery || document.write("{{ HTML::script('js/vendor/jquery-1.9.1.min.js') }}")</script>
{{ HTML::script('js/plugins.js') }}
{{ HTML::script('js/main.js') }}

Change section id="main-content" to

<section id="main-content" class="clearfix">
    @if (Session::has('message'))
      <p class="alert">{{ Session::get('message') }}</p>
    @endif

    @yield('content')
</section><!-- end main-content -->

Creating a migration file

php artisan make:migration create_categories_table

Open /database/migrations/2015_some_number__create_categories_table.php.

public function up()
{
  Schema::create('categories', function($table){
    $table->increments('id');
    $table->string('name');
    $table->timestamps();
  });
}

public function down()
{
  Schema::drop('categories');
}

In terminal

php artisan migrate

Creating category model and controller

Create app/Category.php

class Category extends Eloquent {
  
  protected $fillable = array('name');
  public static $rules = array('name'=>'required|min:3');

}

Create app/HTTP/Controllers/CategoriesController.php

class CategoriesController extends BaseController {

  public function __construct()
  {
    $this->beforeFilter('csrf', array('on'=>'post')); 
  }

  public function getIndex()
  {
    return View::make('categories.index')
      ->with('categories', Category::all());
  }

  public function postCreate()
  {
    $validator= Validator::make(Input::all(), Category::$rules);

    if ($validator->passes())
    {
      $category = new Category;
      $category->name = Input::get('name');
      $category->save();

      return Redirect::to('admin/categories/index')
        ->with('message', 'Category Created');
    }

    return Redirect::to('admin/categories/index')
      ->with('message', 'Something went wrong')
      ->withErrors($validator)
      ->withInput();      
   }

  public function postDestroy()
  {
    $category = Category::find(Input::get('id'));

    if($category)
    {
      $category->delete();
      return Redirect::to('admin/categories/index')
        ->with('message', 'Category Deleted');

      return Redirect::to('admin/categories/index')
        ->with('message', 'Something went wrong, please try again');

    }
  
  }
}

In app/HTTP/routes.php

Route::controller('admin/categories', 'CategoriesController');

Create resources/views/categories/index.blade.php

@extends('layouts.main')

@section('content')

    <div id="admin">

            <h1>Categories Admin Panel</h1><hr>

            <p>Here you can view, delete, and create new categories.</p>

            <h2>Categories</h2><hr>

            <ul>
                    @foreach($categories as $category)
                            <li>
                                    {{ $category->name }} - 
                                    {{ Form::open(array('url'=>'admin/categories/destroy', 'class'=>'form-inline')) }}
                                    {{ Form::hidden('id', $category->id) }}
                                    {{ Form::submit('delete') }}
                                    {{ Form::close() }}
                            </li>
                    @endforeach
            </ul>

            <h2>Create New Category</h2><hr>

            @if($errors->has())
            <div id="form-errors">
                    <p>The following errors have occurred:</p>

                    <ul>
                            @foreach($errors->all() as $error)
                                    <li>{{ $error }}</li>
                            @endforeach
                    </ul>
            </div><!-- end form-errors -->
            @endif

            {{ Form::open(array('url'=>'admin/categories/create')) }}
            <p>
                    {{ Form::label('name') }}
                    {{ Form::text('name') }}
            </p>
            {{ Form::submit('Create Category', array('class'=>'secondary-cart-btn')) }}
            {{ Form::close() }}
    </div><!-- end admin -->

@stop

In terminal

php artisan serve

5. Laravel-eCommerce.md


  1. Getting started

Clone this wiki locally