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

فهاد الجزء الثاني من une application de gestion d'un restaurant ب laravel غادي نكملوا ونزيدو les tables ول ventes ونشوفوا كيفاش نربطوا مابينهم وبين ل menu ومن بعد غادي نزيدو les serveurs لي هما المستخدمين.

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


1- إضافة ل Table Model


زيد model جديد سميه Table فيه غادي تكون la relation مع ل sales وغادي نبدلوا ل option ديال recherche من ل id ل slug الكود لي غادي تزيد فل model Table هو :

                                    
                                        <?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Table extends Model
{
    //
    protected $fillable = ["name", "status", "slug"];

    public function getRouteKeyName()
    {
        return "slug";
    }

    public function sales()
    {
        return $this->belongsToMany(Sales::class);
    }
}
                                    
                                

2- إضافة TableController


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

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

                                    
                                        <?php

namespace App\Http\Controllers;

use App\Table;
use Illuminate\Support\Str;
use Illuminate\Http\Request;

class TableController extends Controller
{
    public function __construct()
    {
        $this->middleware("auth");
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        return view("managments.tables.index")->with([
            "tables" => Table::paginate(5)
        ]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
        return view("managments.tables.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, [
            "name" => "required|unique:tables,name",
            "status" => "required|boolean"
        ]);
        //store data
        $name = $request->name;
        Table::create([
            "name" => $name,
            "slug" => Str::slug($name),
            "status" => $request->status,
        ]);
        //redirect user
        return redirect()->route("tables.index")->with([
            "success" => "table ajoutée avec succés"
        ]);
    }

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

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Table  $table
     * @return \Illuminate\Http\Response
     */
    public function edit(Table $table)
    {
        //
        return view("managments.tables.edit")->with([
            "table" => $table
        ]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Table  $table
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Table $table)
    {
        //
        //validation
        $this->validate($request, [
            "name" => "required|unique:tables,name," . $table->id,
            "status" => "required|boolean"
        ]);
        //store data
        $name = $request->name;
        $table->update([
            "name" => $name,
            "slug" => Str::slug($name),
            "status" => $request->status,
        ]);
        //redirect user
        return redirect()->route("tables.index")->with([
            "success" => "table modifiée avec succés"
        ]);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Table  $table
     * @return \Illuminate\Http\Response
     */
    public function destroy(Table $table)
    {
        //
        $table->delete();
        //redirect user
        return redirect()->route("tables.index")->with([
            "success" => "table supprimée avec succés"
        ]);
    }
}
                                    
                                

3- إضافة ل Servants Model


زيد model جديد سميه Servants فيه غادي تكون la relation مع ل sales الكود لي غادي تزيد فل model Servants هو :

                                      
                                        <?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Servants extends Model
{
    //
    protected $fillable = ["name", "address"];

    public function sales()
    {
        return $this->hasMany(Sales::class);
    }
}
                                      
                                    

4- إضافة ServantsController


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

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

                                        
                                            <?php

namespace App\Http\Controllers;

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

class ServantsController extends Controller
{
    public function __construct()
    {
        $this->middleware("auth");
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        return view("managments.serveurs.index")->with([
            "servants" => Servants::paginate(5)
        ]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
        return view("managments.serveurs.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, [
            "name" => "required|min:3"
        ]);
        //store data
        Servants::create([
            "name" => $request->name,
            "address" => $request->address
        ]);
        //redirect user
        return redirect()->route("servants.index")->with([
            "success" => "serveur ajouté avec succés"
        ]);
    }

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

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Servants  $servants
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
        return view("managments.serveurs.edit")->with([
            "servant" => Servants::findOrFail($id)
        ]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Servants  $servants
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
        //validation
        $this->validate($request, [
            "name" => "required|min:3"
        ]);
        //update data
        $servant = Servants::findOrFail($id);
        $servant->update([
            "name" => $request->name,
            "address" => $request->address
        ]);
        //redirect user
        return redirect()->route("servants.index")->with([
            "success" => "serveur modifié avec succés"
        ]);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Servants  $servants
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $servant = Servants::findOrFail($id);
        $servant->delete();
        //redirect user
        return redirect()->route("servants.index")->with([
            "success" => "serveur supprimé avec succés"
        ]);
    }
}
                                        
                                    

5- إضافة PaymentController


زيد controller سميه PaymentController فيه fonction وحدة لي كاتدينا للفورم ديال إضافة vente ولي غادي نزيدوها من بعد.

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

                                        
                                            <?php

namespace App\Http\Controllers;

use App\Category;
use App\Servants;
use App\Table;
use Illuminate\Http\Request;

class PaymentController extends Controller
{
    //
    public function index()
    {
        return view("payments.index")->with([
            "tables" => Table::all(),
            "categories" => Category::all(),
            "servants" => Servants::all()
        ]);
    }
}
                                        
                                    

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