Restaurant Management System ( Part 5 )

Published Date: 11-Jan-2018 | Tags: Laravel 5.5  Bootstrap 3  Project  

This tutorial I want to show you how to manage Customer with discount in Admin.

Note: I recommend you to check all pervious tutorials before coming to this step. Because some tutorials have relationship with others.

 

Step 1: Create Model

Create model Customer.php in /demo/app.

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Customer extends Model
{
}

 

Step 2: Create View

Create view index.blade.php in /resources/views/customer. (create folder customer if it does not exist) 

<h1 class="page-header">Customer
    <div class="pull-right">
        <a href="javascript:ajaxLoad('customer/create')" class="btn btn-primary pull-right"><i
                    class="glyphicon glyphicon-plus-sign"></i> New</a>
    </div>
</h1>
<div class="col-sm-4 form-group">
    <div class="input-group">
        <input class="form-control" id="search" value="{{ Session::get('customer_search') }}"
               onkeydown="if (event.keyCode == 13) ajaxLoad('{{url('customer')}}?ok=1&search='+this.value)"
               placeholder="Search..."
               type="text">

        <div class="input-group-btn">
            <button type="button" class="btn btn-default"
                    onclick="ajaxLoad('{{url('customer')}}?ok=1&search='+$('#search').val())"><i
                        class="glyphicon glyphicon-search"></i>
            </button>
        </div>
    </div>
</div>
<table class="table table-bordered table-striped">
    <thead>
    <tr>
        <th width="50px" style="text-align: center">No</th>
        <th>
            <a href="javascript:ajaxLoad('customer?field=name&sort={{Session::get("customer_sort")=="asc"?"desc":"asc"}}')">
                Name
            </a>
            <i style="font-size: 12px"
               class="glyphicon  {{ Session::get('customer_field')=='name'?(Session::get('customer_sort')=='asc'?'glyphicon-sort-by-alphabet':'glyphicon-sort-by-alphabet-alt'):'' }}">
            </i>
        </th>
        <th style="text-align: right" width="100px">Discount</th>
        <th width="140px"></th>
    </tr>
    </thead>
    <tbody>
    <?php $i = 1;?>
    @foreach($customers as $key=>$customer)
        <tr>
            <td align="center">{{$i++}}</td>
            <td>{{$customer->name}}</td>
            <td align="right">{{$customer->discount}} %</td>
            <td style="text-align: center">
                <a class="btn btn-primary btn-xs" title="Edit"
                   href="javascript:ajaxLoad('customer/update/{{$customer->id}}')">
                    <i class="glyphicon glyphicon-edit"></i> Edit</a>
                <a class="btn btn-danger btn-xs" title="Delete"
                   href="javascript:if(confirm('Are you sure want to delete?')) ajaxDelete('customer/delete/{{$customer->id}}','{{csrf_token()}}')">
                    <i class="glyphicon glyphicon-trash"></i> Delete
                </a>
            </td>
        </tr>
    @endforeach
    </tbody>
</table>
<div class="pull-right">{!! str_replace('/?','?',$customers->render()) !!}</div>
<div class="row">
    <i class="col-sm-12">
        Total: {{$customers->total()}} records
    </i>
</div>

 

Create view form.blade.php in /resources/views/customer. (create folder customer if it does not exist)

<h2 class="page-header">{{isset($customer)?'Edit':'New'}} Customer</h2>
@if(isset($customer))
    {!! Form::model($customer,["id"=>"frm","class"=>"form-horizontal","method"=>"put"]) !!}
@else
    {!! Form::open(["id"=>"frm","class"=>"form-horizontal"]) !!}
@endif
<div class="form-group required" id="form-name-error">
    {!! Form::label("name","Name",["class"=>"control-label col-md-2"]) !!}
    <div class="col-md-5">
        {!! Form::text("name",null,["class"=>"form-control required","id"=>"focus"]) !!}
        <span id="name-error" class="help-block"></span>
    </div>
</div>
<div class="form-group required" id="form-discount-error">
    {!! Form::label("discount","Discount",["class"=>"control-label col-md-2"]) !!}
    <div class="col-md-5">
        {!! Form::text("discount",null,["class"=>"form-control required"]) !!}
        <span id="discount-error" class="help-block"></span>
    </div>
</div>
<div class="form-group">
    <div class="col-md-5 col-md-push-2">
        <a href="javascript:ajaxLoad('customer')" class="btn btn-danger"><i class="glyphicon glyphicon-backward"></i>
            Back</a>
        {!! Form::button("<i class='glyphicon glyphicon-floppy-disk'></i> Save",["type" => "submit","class"=>"btn
    btn-primary","id"=>"btn_save"])!!}
    </div>
</div>
{!! Form::close() !!}

 

Step 3: Create Controller

Create controller CustomerController.php in /demo/app/Http/Cotrollers.

<?php
namespace App\Http\Controllers;

use App\Customer;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Validator;

class CustomerController extends Controller
{
    public function index()
    {
        Session::put('customer_search', Input::has('ok') ? Input::get('search') : (Session::has('customer_search') ? Session::get('customer_search') : ''));
        Session::put('customer_field', Input::has('field') ? Input::get('field') : (Session::has('customer_field') ? Session::get('customer_field') : 'name'));
        Session::put('customer_sort', Input::has('sort') ? Input::get('sort') : (Session::has('customer_sort') ? Session::get('customer_sort') : 'asc'));
        $customers = new Customer();
        $customers = $customers->where('name', 'like', '%' . Session::get('customer_search') . '%')->orderBy(Session::get('customer_field'), Session::get('customer_sort'))->paginate(20);
        return view('customer.index', ['customers' => $customers]);
    }

    public function update(Request $request, $id)
    {
        if ($request->isMethod('get'))
            return view('customer.form', ['customer' => Customer::find($id)]);
        else {
            $customer = Customer::find($id);
            $rules = ["discount" => "numeric|min:0|max:100"];
            if ($customer->name != Input::get('name'))
                $rules += ['name' => 'required|unique:customers'];
            $validator = Validator::make(Input::all(), $rules);
            if ($validator->fails()) {
                return array(
                    'fail' => true,
                    'errors' => $validator->getMessageBag()->toArray()
                );
            }
            $customer->name = Input::get('name');
            $customer->discount = Input::get('discount');
            $customer->user_id = Auth::user()->id;
            $customer->save();
            Session::put('msg_status', true);
        }
    }

    public function create(Request $request)
    {
        if ($request->isMethod('get'))
            return view('customer.form');
        else {
            $validator = Validator::make(Input::all(), [
                "name" => "required|unique:customers",
                "discount" => "numeric|min:0|max:100"
            ]);
            if ($validator->fails()) {
                return array(
                    'fail' => true,
                    'errors' => $validator->getMessageBag()->toArray()
                );
            }
            $customer = new Customer();
            $customer->name = Input::get('name');
            $customer->discount = Input::get('discount');
            $customer->user_id = Auth::user()->id;
            $customer->save();
            Session::put('msg_status', true);
        }
    }

    public function delete($id)
    {
        Customer::destroy($id);
        return redirect('customer');
    }

}

 

Step 4: How to Test?

After login to admin screen, you will be able to access Customer option on the left side menu.

customer-management

Hope you can do it.


Demo


Related Posts

© 2018 All Rights Reserved.