Restaurant Management System ( Part 6 )

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

This tutorial I want to show you how to manage Item Categories 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 ItemCategory.php in /demo/app.

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class ItemCategory extends Model
{
}

 

Step 2: Create View

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

<h1 class="page-header">Item Category
    <div class="pull-right">
        <a href="javascript:ajaxLoad('item_category/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('item_category_search') }}"
               onkeydown="if (event.keyCode == 13) ajaxLoad('{{url('item_category')}}?ok=1&search='+this.value)"
               placeholder="Search..."
               type="text">

        <div class="input-group-btn">
            <button type="button" class="btn btn-default"
                    onclick="ajaxLoad('{{url('item_category')}}?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('item_category?field=name&sort={{Session::get("item_category_sort")=="asc"?"desc":"asc"}}')">
                Name
            </a>
            <i style="font-size: 12px"
               class="glyphicon  {{ Session::get('item_category_field')=='name'?(Session::get('item_category_sort')=='asc'?'glyphicon-sort-by-alphabet':'glyphicon-sort-by-alphabet-alt'):'' }}">
            </i>
        </th>
        <th width="140px"></th>
    </tr>
    </thead>
    <tbody>
    <?php $i = 1;?>
    @foreach($item_categories as $key=>$item_category)
        <tr>
            <td align="center">{{$i++}}</td>
            <td>{{$item_category->name}}</td>
            <td style="text-align: center">
                <a class="btn btn-primary btn-xs" title="Edit"
                   href="javascript:ajaxLoad('item_category/update/{{$item_category->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('item_category/delete/{{$item_category->id}}','{{csrf_token()}}')">
                    <i class="glyphicon glyphicon-trash"></i> Delete
                </a>
            </td>
        </tr>
    @endforeach
    </tbody>
</table>
<div class="pull-right">{!! str_replace('/?','?',$item_categories->render()) !!}</div>
<div class="row">
    <i class="col-sm-12">
        Total: {{$item_categories->total()}} records
    </i>
</div>

 

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

<h2 class="page-header">{{isset($item_category)?'Edit':'New'}} Item Category</h2>
@if(isset($item_category))
    {!! Form::model($item_category,["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-3"]) !!}
    <div class="col-md-6">
        {!! Form::text("name",null,["class"=>"form-control required","id"=>"focus"]) !!}
        <span id="name-error" class="help-block"></span>
    </div>
</div>
<div class="form-group">
    <div class="col-md-6 col-md-push-3">
        <a href="javascript:ajaxLoad('item_category')" 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 ItemCategoryController.php in /demo/app/Http/Cotrollers.

<?php
namespace App\Http\Controllers;

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

class ItemCategoryController extends Controller
{
    public function index()
    {
        Session::put('item_category_search', Input::has('ok') ? Input::get('search') : (Session::has('item_category_search') ? Session::get('item_category_search') : ''));
        Session::put('item_category_field', Input::has('field') ? Input::get('field') : (Session::has('item_category_field') ? Session::get('item_category_field') : 'name'));
        Session::put('item_category_sort', Input::has('sort') ? Input::get('sort') : (Session::has('item_category_sort') ? Session::get('item_category_sort') : 'asc'));
        $item_categories = ItemCategory::where('name', 'like', '%' . Session::get('item_category_search') . '%')->orderBy(Session::get('item_category_field'), Session::get('item_category_sort'))->paginate(20);
        return view('item_category.index', ['item_categories' => $item_categories]);
    }

    public function update(Request $request, $id)
    {
        if ($request->isMethod('get'))
            return view('item_category.form', ['item_category' => ItemCategory::find($id)]);
        else {
            $item_category = ItemCategory::find($id);
            if (strtolower($item_category->name) != strtolower(Input::get('name'))) {
                $validator = Validator::make(Input::all(), [
                    'name' => 'required|unique:item_categories'
                ]);
                if ($validator->fails()) {
                    return array(
                        'fail' => true,
                        'errors' => $validator->getMessageBag()->toArray()
                    );
                }
            }
            $item_category->name = Input::get('name');
            $item_category->save();
            Session::put('msg_status', true);
        }
    }

    public function create(Request $request)
    {
        if ($request->isMethod('get'))
            return view('item_category.form');
        else {
            $validator = Validator::make(Input::all(), [
                "name" => "required|unique:item_categories"
            ]);
            if ($validator->fails()) {
                return array(
                    'fail' => true,
                    'errors' => $validator->getMessageBag()->toArray()
                );
            }
            $item_category = new ItemCategory();
            $item_category->name = Input::get('name');
            $item_category->save();
            Session::put('msg_status', true);
        }
    }

    public function delete($id)
    {
        ItemCategory::destroy($id);
        return redirect('item_category');
    }

}

 

Step 4: How to Test?

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

item-categories

Hope you can do it.


Demo


Related Posts

© 2018 All Rights Reserved.