Restaurant Management System ( Part 10 )

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

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

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Recipe extends Model
{
    public function item()
    {
        return $this->belongsTo('App\Item');
    }
    public function product()
    {
        return $this->belongsTo('App\Product');
    }
}

 

Step 2: Create View

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

<h1 class="page-header">Recipe</h1>
<div class="row">
    <div class="col-md-4 form-group">
        {!! Form::select('category',['-1'=>'Select Category']+App\ProductCategory::orderBy('name')->pluck('name','id')->toArray() ,Session::get('recipe_category'),['class'=>'form-control','style'=>'height:40px','onChange'=>'ajaxLoad("'.url("recipe").'?category="+this.value)']) !!}
    </div>
    <div class="col-md-6 form-group">
        {!! Form::select('product_id',$products ,Session::get('recipe_product_id'),['class'=>'form-control','style'=>'height:40px','onChange'=>'ajaxLoad("'.url("recipe").'?product_id="+this.value)']) !!}
    </div>
</div>
@if($product)
    <b style="font-size: 14px;color: red">{{$product->name}}</b>
    <div class="pull-right">
        <a href="javascript:ajaxLoad('recipe/create')" class="btn btn-primary btn-xs"><i
                    class="glyphicon glyphicon-plus-sign"></i> New</a>
    </div>

    <table class="table table-bordered table-striped" style="margin-top: 10px">
        <thead>
        <tr>
            <th width="50px" style="text-align: center">No</th>
            <th>Name</th>
            <th style="text-align: right">Quantity</th>
            <th style="text-align: center">Unit</th>
            <th width="140px"></th>
        </tr>
        </thead>
        <tbody>
        <?php $i = 1;?>
        @foreach($product->recipes as $key=>$recipe)
            <tr>
                <td align="center">{{$i++}}</td>
                <td>{{$recipe->item?$recipe->item->name:''}}</td>
                <td align="right">{{$recipe->quantity}}</td>
                <td align="center">{{$recipe->item?$recipe->item->unit:''}}</td>
                <td style="text-align: center">
                    <a class="btn btn-primary btn-xs" title="Edit"
                       href="javascript:ajaxLoad('recipe/update/{{$recipe->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('recipe/delete/{{$recipe->id}}','{{csrf_token()}}')">
                        <i class="glyphicon glyphicon-trash"></i> Delete
                    </a>
                </td>
            </tr>
        @endforeach
        </tbody>
    </table>
@endif

 

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

<h2 class="page-header">{{isset($recipe)?'Add':'Edit'}} Recipe</h2>
@if(isset($recipe))
    {!! Form::model($recipe,["id"=>"frm","class"=>"form-horizontal","method"=>"put"]) !!}
@else
    {!! Form::open(["id"=>"frm","class"=>"form-horizontal"]) !!}
@endif
<div class="form-group">
    {!! Form::label("item_id","Item Name",["class"=>"control-label col-md-2"]) !!}
    <div class="col-md-5">
        {!! Form::select("item_id",\App\Item::orderBy('name')->pluck('name','id'),null,["class"=>"form-control required","id"=>"name"]) !!}
    </div>
</div>
<div class="form-group required" id="form-quantity-error">
    {!! Form::label("quantity","Quantity",["class"=>"control-label col-md-2"]) !!}
    <div class="col-md-5">
        {!! Form::text("quantity",null,["class"=>"form-control required","id"=>"focus"]) !!}
        <span id="quantity-error" class="help-block"></span>
    </div>
</div>
<div class="form-group">
    {!! Form::label("unit","Unit",["class"=>"control-label col-md-2"]) !!}
    <div class="col-md-5">
        {!! Form::text("unit",null,["class"=>"form-control","id"=>"unit","readonly"]) !!}
    </div>
</div>
<div class="form-group">
    <div class="col-md-6 col-md-push-3">
        <a href="javascript:ajaxLoad('recipe')" 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() !!}
<script>
    $('#name').on('change', function () {
        getUnits($(this).val());
    });
    function getUnits(item_id) {
        $.ajax({
            url: "{{url('item/unit')}}/" + item_id,
            success: function (data) {
                $('#unit').val(data);
            }
        });
    }
    $(document).ready(function () {
        getUnits($('#name').val());
    });
</script>

 

Step 3: Create Controller

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

<?php
namespace App\Http\Controllers;

use App\Item;
use App\Product;
use App\Recipe;
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 RecipeController extends Controller
{
    public function index()
    {
        Session::put('recipe_category', Input::has('category') ? Input::get('category') : (Session::has('recipe_category') ? Session::get('recipe_category') : -1));
        Session::put('recipe_type', Input::has('type') ? Input::get('type') : (Session::has('recipe_type') ? Session::get('recipe_type') : -1));
        Session::put('recipe_product_id', Input::has('product_id') ? Input::get('product_id') : (Session::has('recipe_product_id') ? Session::get('recipe_product_id') : -1));

        if (Session::get('recipe_category') == -1 and Session::get('recipe_type') == -1)
            $products = [];
        else {
            $products = new Product();
            if (Session::get('recipe_category') != -1)
                $products = $products->where('product_category_id', Session::get('recipe_category'));
            if (Session::get('recipe_type') != -1)
                $products = $products->where('product_type_id', Session::get('recipe_type'));
            $products = $products->orderBy('name')->pluck('name', 'id')->toArray();
        }
        return view('recipe.index', ['products' => ['-1' => ''] + $products, 'product' => Product::find(Session::get('recipe_product_id'))]);
    }

    public function update(Request $request, $id)
    {
        if ($request->isMethod('get'))
            return view('recipe.form', ['recipe' => Recipe::find($id)]);
        else {
            $validator = Validator::make(Input::all(), [
                "quantity" => "required|numeric"
            ]);
            if ($validator->fails()) {
                return array(
                    'fail' => true,
                    'errors' => $validator->getMessageBag()->toArray()
                );
            }
            $recipe = Recipe::find($id);
            $recipe->product_id = Session::get('recipe_product_id');
            $recipe->item_id = Input::get('item_id');
            $recipe->quantity = Input::get('quantity');
            $recipe->user_id = Auth::user()->id;
            $recipe->save();
            Session::put('msg_status', true);
        }
    }


    public function create(Request $request)
    {
        if ($request->isMethod('get'))
            return view('recipe.form');
        else {
            $validator = Validator::make(Input::all(), [
                "quantity" => "required|numeric"
            ]);
            if ($validator->fails()) {
                return array(
                    'fail' => true,
                    'errors' => $validator->getMessageBag()->toArray()
                );
            }
            $recipe = new Recipe();
            $recipe->product_id = Session::get('recipe_product_id');
            $recipe->item_id = Input::get('item_id');
            $recipe->quantity = Input::get('quantity');
            $recipe->user_id = Auth::user()->id;
            $recipe->save();
            Session::put('msg_status', true);
        }
    }

    public function delete($id)
    {
        Recipe::destroy($id);
        return redirect('recipe');
    }

}

 

Step 4: How to Test?

After login to admin screen, you will be able to access Recipe, Product, and Product Category options on the left side menu without error more. 

recipe

Hope you can do it.


Demo


Related Posts

© 2018 All Rights Reserved.