Application de gestion d'un restaurant ب laravel الجزء الثالت

فهاد الجزء الثالت من une application de gestion d'un restaurant ب laravel غادي نكملوا ونزيدو les ventes ديالنا ومنبعد غادي ندوزو لعرض رسائل التنبيه (erreurs de validation) وغادي نزيدو القائمة الجانبية.  


نظرة سريعة بالفيديو


1- إضافة ل Sales Model


زيد model جديد سميه Sales فيه غادي تكون la relation مع tables ل menus و serveur الكود لي غادي تزيد فل model Sales هو :


                                    
                                        <?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Sales extends Model
{
    //
    protected $fillable = [
        "user_id", "quantity", "total_price",
        "total_received", "change", "payment_type", "payment_status"
    ];

    public function menus()
    {
        return $this->belongsToMany(Menu::class);
    }

    public function tables()
    {
        return $this->belongsToMany(Table::class);
    }

    public function servant()
    {
        return $this->belongsTo(Servants::class);
    }
}
                                    
                                

2- إضافة SalesController


زيد controller سميه SalesController فيه les fonctions ديال الإضافة التعديل والحذف ديال vente.

الكود ديال الملف هو :

                                    
                                        <?php

namespace App\Http\Controllers;

use App\Sales;
use App\Servants;
use Illuminate\Http\Request;

class SalesController extends Controller
{
    public function __construct()
    {
        $this->middleware("auth");
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        $sales = Sales::orderBy("created_at", "DESC")->paginate(10);
        return view("sales.index")->with([
            "sales" => $sales
        ]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //validation
        $this->validate($request, [
            "table_id" => "required",
            "menu_id" => "required",
            "servant_id" => "required",
            "quantity" => "required|numeric",
            "total_price" => "required|numeric",
            "total_received" => "required|numeric",
            "change" => "required|numeric",
            "payment_type" => "required",
            "payment_status" => "required",
        ]);
        //store data
        $sale = new Sales();
        $sale->servant_id = $request->servant_id;
        $sale->quantity = $request->quantity;
        $sale->total_price = $request->total_price;
        $sale->total_received = $request->total_received;
        $sale->change = $request->change;
        $sale->payment_status = $request->payment_status;
        $sale->payment_type = $request->payment_type;
        $sale->save();
        $sale->menus()->sync($request->menu_id);
        $sale->tables()->sync($request->table_id);
        //redirect user
        return redirect()->back()->with([
            "success" => "Paiement effectué avec succés"
        ]);
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Sales  $sales
     * @return \Illuminate\Http\Response
     */
    public function show(Sales $sales)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Sales  $sales
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //get sale to update
        $sales = Sales::findOrFail($id);
        //get sale tables
        $tables = $sales->tables()->where('sales_id', $sales->id)->get();
        //get table menus
        $menus = $sales->menus()->where('sales_id', $sales->id)->get();
        return view("sales.edit")->with([
            "tables" => $tables,
            "menus" => $menus,
            "sale" => $sales,
            "servants" => Servants::all()
        ]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Sales  $sales
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //validation
        $this->validate($request, [
            "table_id" => "required",
            "menu_id" => "required",
            "servant_id" => "required",
            "quantity" => "required|numeric",
            "total_price" => "required|numeric",
            "total_received" => "required|numeric",
            "change" => "required|numeric",
            "payment_type" => "required",
            "payment_status" => "required",
        ]);
        //get sale to update
        $sale = Sales::findOrFail($id);
        //update data
        $sale->servant_id = $request->servant_id;
        $sale->quantity = $request->quantity;
        $sale->total_price = $request->total_price;
        $sale->total_received = $request->total_received;
        $sale->change = $request->change;
        $sale->payment_status = $request->payment_status;
        $sale->payment_type = $request->payment_type;
        $sale->update();
        $sale->menus()->sync($request->menu_id);
        $sale->tables()->sync($request->table_id);
        //redirect user
        return redirect()->back()->with([
            "success" => "Paiement modifié avec succés"
        ]);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Sales  $sales
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //get sale to update
        $sale = Sales::findOrFail($id);
        //delete sales
        $sale->delete();
        //redirect user
        return redirect()->back()->with([
            "success" => "Paiement supprimé avec succés"
        ]);
    }
}
                                    
                                

3- إضافة رسائل التنبيه


ف dossier views/layouts زيد fichier جديد سميه alerts.blade.php فيه غادي يكون الكود ديال عرض رسائل التنبيه مثلا يلا كان شي خطأ فالإضافة التعديل أو غيرو.

الكود ديال الملف هو :

                                      
                                        @if ($errors->all())
    @foreach ($errors->all() as $error)
        <div class="alert alert-danger">
            {{ $error }}
        </div>
    @endforeach
@endif

@if (session()->has("success"))
    <div class="alert alert-success alert-dismissible fade show" role="alert">
        <strong>{{ session()->get("success") }}</strong>
        <button type="button"
            data-dismiss="alert"
            class="close"
            aria-label="close">
            <span>×</span>
        </button>
    </div>
@endif
@if (session()->has("warning"))
    <div class="alert alert-warning alert-dismissible fade show" role="alert">
        <strong>{{ session()->get("warning") }}</strong>
        <button type="button"
            data-dismiss="alert"
            class="close"
            aria-label="close"
            >
            <span>×</span>
        </button>
    </div>
@endif
@if (session()->has("info"))
    <div class="alert alert-info alert-dismissible fade show" role="alert">
        <strong>{{ session()->get("info") }}</strong>
        <button type="button"
            data-dismiss="alert"
            class="close"
            aria-label="close"
            >
            <span>×</span>
        </button>
    </div>
@endif
                                      
                                    

4- تعديل محتوى الملف app.blade.php


ف dossier views/layouts غادي نديرو تعديل على الملف app.blade.php لي غادي يكون الكود ديالو بعد التعديل  هو :

                                        
                                            <!doctype html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- CSRF Token -->
    <meta name="csrf-token" content="{{ csrf_token() }}">

    <title>{{ config('app.name', 'Restaurant XXXXX') }}</title>

    <!-- Scripts -->
    <script src="{{ asset('js/app.js') }}" defer></script>

    <!-- Fonts -->
    <link rel="dns-prefetch" href="//fonts.gstatic.com">
    <link href="https://fonts.googleapis.com/css?family=Nunito" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.1/css/all.min.css" integrity="sha512-+4zCK9k+qNFUR5X+cKL9EIR+ZOhtIloNl9GIKS57V1MyNsYpYcUrUeQc9vNfzsWfV28IaLL3i96P9sdNyeRssA==" crossorigin="anonymous" />
    <!-- Styles -->
    <link href="{{ asset('css/app.css') }}" rel="stylesheet">
</head>
<body>
    <div id="app">
        <nav class="navbar navbar-expand-md navbar-light bg-white shadow-sm">
            <div class="container">
                <a class="navbar-brand" href="{{ url('/') }}">
                    <i class="fas fa-hamburger"></i> Restaurant XXXXX
                </a>
                <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="{{ __('Toggle navigation') }}">
                    <span class="navbar-toggler-icon"></span>
                </button>

                <div class="collapse navbar-collapse" id="navbarSupportedContent">
                    <!-- Left Side Of Navbar -->
                    <ul class="navbar-nav mr-auto">

                    </ul>

                    <!-- Right Side Of Navbar -->
                    <ul class="navbar-nav ml-auto">
                        <!-- Authentication Links -->
                        @guest
                            <li class="nav-item">
                                <a class="nav-link" href="{{ route('login') }}">{{ __('Login') }}</a>
                            </li>
                            @if (Route::has('register'))
                                <li class="nav-item">
                                    <a class="nav-link" href="{{ route('register') }}">{{ __('Register') }}</a>
                                </li>
                            @endif
                        @else
                            <li class="nav-item dropdown">
                                <a id="navbarDropdown" class="nav-link dropdown-toggle" href="#" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" v-pre>
                                    {{ Auth::user()->name }}
                                </a>

                                <div class="dropdown-menu dropdown-menu-right" aria-labelledby="navbarDropdown">
                                    <a class="dropdown-item" href="{{ route('logout') }}"
                                       onclick="event.preventDefault();
                                                     document.getElementById('logout-form').submit();">
                                        {{ __('Déconnexion') }}
                                    </a>

                                    <form id="logout-form" action="{{ route('logout') }}" method="POST" class="d-none">
                                        @csrf
                                    </form>
                                </div>
                            </li>
                        @endguest
                    </ul>
                </div>
            </div>
        </nav>
        <div class="row">
            <div class="col-md-8 mx-auto my-4">
                @include('layouts.alert')
            </div>
        </div>
        <main class="py-4">
            @yield('content')
        </main>
    </div>
    <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js" integrity="sha384-9/reFTGAW83EW2RDu2S0VKaIzap3H66lZH81PoYlFhbGU+6BZp6G7niu735Sk7lN" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/js/bootstrap.min.js" integrity="sha384-w1Q4orYjBQndcko6MimVbzY0tgp4pWB4lZ7lr30WKz0vr/aWKhXdBNmNb5D92v7s" crossorigin="anonymous"></script>
    @yield('javascript')
</body>
</html>
                                        
                                    

5- إضافة sidebar


دائما ف dossier views/layouts غادي تزيد fichier جديد سميه sidebar.blade.php فيه غادي تكون القائمة الجانبية.

الكود ديال الملف هو :

                                        
                                            <ul class="list-group mb-3">
    <a href="{{ route("categories.index") }}" class="list-group-item
        font-weight-bold list-group-item-action
        list-group-item-light">
        <i class="fas fa-th-list"></i>
            Catégories
    </a>
    <a href="{{ route("menus.index") }}" class="list-group-item
        font-weight-bold list-group-item-action
        list-group-item-light">
        <i class="fas fa-clipboard-list"></i>
            Menus</a>
    <a href="{{ route("tables.index") }}" class="list-group-item
        font-weight-bold list-group-item-action
        list-group-item-light">
        <i class="fas fa-chair"></i>
        Tables</a>
    <a href="{{ route("servants.index") }}" class="list-group-item
        font-weight-bold list-group-item-action
        list-group-item-light">
        <i class="fas fa-user-cog"></i>
        Sérveurs
    </a>
</ul>
                                        
                                    

كلمات مفاتيح :