Application de gestion d'un restaurant ب laravel الجزء الأول

فهاد ال projet الجديد غادي نشوفوا كيفاش نقادو une application de gestion d'un restaurant ب laravel فل application ديالنا كت gérer la caisse فهاد الجزء الأول غادي نقادو قاعدة البيانات ونشوفوا كيفاش نديرو الإتصال بها.

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


1- إضافة قاعدة البيانات


أول حاجة زيد projet جديد سميه لي بغيتي من بعد installer les packages لي غادي نحتاجو غادي تدير هاد les commandes متابعين :

composer require laravel/ui:^2.4

 php artisan ui vue --auth

npm run dev & npm run watch

من بعد غادي تزيد base de données ف phpmyadmin سميها لي بغيتي أنا سميتها laravel-restaurant المحتوى ديالها هو هذا :



                                    
                                        SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";

--
-- Base de données :  `laravel-restaurant`
--

-- --------------------------------------------------------

--
-- Structure de la table `categories`
--

CREATE TABLE `categories` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `title` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `slug` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

--
-- Déchargement des données de la table `categories`
--

INSERT INTO `categories` (`id`, `title`, `slug`, `created_at`, `updated_at`) VALUES
(1, 'Pizzas', 'pizzas', '2020-11-18 12:25:07', '2020-11-18 12:25:07'),
(2, 'Poissons', 'poissons', '2020-11-18 12:25:18', '2020-11-18 12:25:18'),
(3, 'Déjeuner', 'dejeuner', '2020-11-18 12:25:30', '2020-11-18 12:25:30'),
(4, 'Petit déjeuner', 'petit-dejeuner', '2020-11-18 12:25:40', '2020-11-18 12:25:40'),
(5, 'Dînner', 'dinner', '2020-11-18 12:26:03', '2020-11-18 12:26:03'),
(7, 'Salades', 'salades', '2020-12-03 09:16:26', '2020-12-03 09:16:49');

-- --------------------------------------------------------

--
-- Structure de la table `failed_jobs`
--

CREATE TABLE `failed_jobs` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `connection` text COLLATE utf8mb4_unicode_ci NOT NULL,
  `queue` text COLLATE utf8mb4_unicode_ci NOT NULL,
  `payload` longtext COLLATE utf8mb4_unicode_ci NOT NULL,
  `exception` longtext COLLATE utf8mb4_unicode_ci NOT NULL,
  `failed_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Structure de la table `menus`
--

CREATE TABLE `menus` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `title` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `slug` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `description` text COLLATE utf8mb4_unicode_ci NOT NULL,
  `price` decimal(8,2) NOT NULL,
  `image` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `category_id` bigint(20) UNSIGNED NOT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

--
-- Déchargement des données de la table `menus`
--

INSERT INTO `menus` (`id`, `title`, `slug`, `description`, `price`, `image`, `category_id`, `created_at`, `updated_at`) VALUES
(2, 'Hamburger', 'hamburger', 'Hamburger', '50.00', '1606643572_hamburger-1238246_1920.jpg', 3, '2020-11-29 08:52:53', '2020-11-29 08:52:53'),
(3, 'Sandwich', 'sandwich', 'Sandwich', '20.00', '1606643632_fast-food-2132863_1920.jpg', 3, '2020-11-29 08:53:52', '2020-11-29 08:53:52'),
(4, 'Tacos', 'tacos', 'Tacos', '40.00', '1606643681_tacos-1613795_1920.jpg', 5, '2020-11-29 08:54:41', '2020-11-29 08:54:41'),
(5, 'Croissants', 'croissants', 'Croissants', '3.00', '1606643761_bread-4077812_1920.jpg', 4, '2020-11-29 08:56:01', '2020-11-29 08:58:07'),
(6, 'Viande hachée', 'viande-hachee', 'Viande hachée', '40.00', '1606643828_minced-meat-4455072_1920.jpg', 5, '2020-11-29 08:57:08', '2020-11-29 08:57:08'),
(7, 'Brochette', 'brochette', 'Brochette', '50.00', '1606643866_barbecue-84671_1920.jpg', 3, '2020-11-29 08:57:46', '2020-11-29 08:57:46'),
(8, 'Pizza', 'pizza', 'Pizza au fromage', '60.00', '1606643938_cheese-1869708_1920.jpg', 1, '2020-11-29 08:58:58', '2020-11-29 08:58:58'),
(9, 'Poulet frit', 'poulet-frit', 'Poulet frit', '40.00', '1606654951_fried-chicken-250863_1920.jpg', 5, '2020-11-29 12:02:31', '2020-11-29 12:02:31');

-- --------------------------------------------------------

--
-- Structure de la table `menu_sales`
--

CREATE TABLE `menu_sales` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `menu_id` bigint(20) UNSIGNED NOT NULL,
  `sales_id` bigint(20) UNSIGNED NOT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

--
-- Déchargement des données de la table `menu_sales`
--

INSERT INTO `menu_sales` (`id`, `menu_id`, `sales_id`, `created_at`, `updated_at`) VALUES
(1, 7, 13, NULL, NULL),
(3, 8, 14, NULL, NULL),
(6, 6, 16, NULL, NULL),
(7, 2, 17, NULL, NULL),
(8, 3, 18, NULL, NULL),
(9, 4, 19, NULL, NULL),
(10, 8, 20, NULL, NULL),
(11, 6, 21, NULL, NULL),
(12, 9, 22, NULL, NULL),
(14, 8, 23, NULL, NULL),
(15, 5, 24, NULL, NULL),
(16, 2, 25, NULL, NULL),
(17, 3, 25, NULL, NULL);

-- --------------------------------------------------------

--
-- Structure de la table `migrations`
--

CREATE TABLE `migrations` (
  `id` int(10) UNSIGNED NOT NULL,
  `migration` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `batch` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

--
-- Déchargement des données de la table `migrations`
--

INSERT INTO `migrations` (`id`, `migration`, `batch`) VALUES
(1, '2014_10_12_000000_create_users_table', 1),
(2, '2014_10_12_100000_create_password_resets_table', 1),
(3, '2019_08_19_000000_create_failed_jobs_table', 1),
(6, '2020_11_18_105209_create_categories_table', 2),
(8, '2020_11_19_091207_create_menu_table', 3),
(9, '2020_11_19_123414_create_tables_table', 4),
(10, '2020_11_20_120238_create_sales_table', 5),
(11, '2020_11_20_125921_create_menu_sales_table', 5),
(12, '2020_11_20_130035_create_sales_table_table', 5),
(14, '2020_11_29_101142_create_servants_table', 6),
(15, '2020_11_29_111539_update_sales_table', 7);

-- --------------------------------------------------------

--
-- Structure de la table `password_resets`
--

CREATE TABLE `password_resets` (
  `email` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
  `token` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `created_at` timestamp NULL DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Structure de la table `sales`
--

CREATE TABLE `sales` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `servant_id` bigint(20) UNSIGNED NOT NULL,
  `quantity` int(11) NOT NULL DEFAULT '1',
  `total_price` decimal(8,2) NOT NULL DEFAULT '0.00',
  `total_received` decimal(8,2) NOT NULL DEFAULT '0.00',
  `change` decimal(8,2) NOT NULL DEFAULT '0.00',
  `payment_type` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'cash',
  `payment_status` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'unpaid',
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

--
-- Déchargement des données de la table `sales`
--

INSERT INTO `sales` (`id`, `servant_id`, `quantity`, `total_price`, `total_received`, `change`, `payment_type`, `payment_status`, `created_at`, `updated_at`) VALUES
(13, 1, 1, '80.00', '80.00', '0.00', 'cash', 'paid', '2020-11-29 09:00:56', '2020-11-29 09:00:56'),
(14, 1, 2, '120.00', '120.00', '0.00', 'cash', 'paid', '2020-11-29 09:04:07', '2020-11-29 09:04:07'),
(16, 1, 2, '40.00', '80.00', '0.00', 'cash', 'paid', '2020-11-29 11:38:53', '2020-11-29 11:38:53'),
(17, 1, 2, '50.00', '100.00', '0.00', 'cash', 'paid', '2020-11-29 11:41:45', '2020-11-29 11:41:45'),
(18, 1, 3, '20.00', '60.00', '0.00', 'cash', 'paid', '2020-11-29 11:42:55', '2020-11-29 11:42:55'),
(19, 1, 2, '40.00', '80.00', '0.00', 'cash', 'paid', '2020-11-30 08:44:08', '2020-11-30 08:44:08'),
(20, 1, 2, '60.00', '120.00', '0.00', 'cash', 'paid', '2020-11-30 10:30:54', '2020-11-30 10:30:54'),
(21, 1, 2, '40.00', '80.00', '0.00', 'cash', 'paid', '2020-11-30 10:39:39', '2020-11-30 10:39:39'),
(22, 1, 2, '70.00', '70.00', '0.00', 'cash', 'paid', '2020-11-30 10:40:45', '2020-11-30 10:42:07'),
(23, 1, 2, '60.00', '120.00', '0.00', 'cash', 'paid', '2020-12-01 11:16:44', '2020-12-01 11:16:44'),
(24, 1, 6, '3.00', '18.00', '0.00', 'cash', 'paid', '2020-12-03 09:19:51', '2020-12-03 09:20:37'),
(25, 1, 1, '70.00', '70.00', '0.00', 'cash', 'paid', '2020-12-03 09:21:44', '2020-12-03 09:21:44');

-- --------------------------------------------------------

--
-- Structure de la table `sales_table`
--

CREATE TABLE `sales_table` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `sales_id` bigint(20) UNSIGNED NOT NULL,
  `table_id` bigint(20) UNSIGNED NOT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

--
-- Déchargement des données de la table `sales_table`
--

INSERT INTO `sales_table` (`id`, `sales_id`, `table_id`, `created_at`, `updated_at`) VALUES
(1, 13, 2, NULL, NULL),
(2, 14, 3, NULL, NULL),
(4, 16, 4, NULL, NULL),
(5, 17, 4, NULL, NULL),
(6, 18, 5, NULL, NULL),
(7, 19, 7, NULL, NULL),
(8, 20, 3, NULL, NULL),
(9, 21, 3, NULL, NULL),
(10, 22, 4, NULL, NULL),
(11, 23, 2, NULL, NULL),
(12, 24, 2, NULL, NULL),
(13, 25, 2, NULL, NULL);

-- --------------------------------------------------------

--
-- Structure de la table `servants`
--

CREATE TABLE `servants` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `address` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

--
-- Déchargement des données de la table `servants`
--

INSERT INTO `servants` (`id`, `name`, `address`, `created_at`, `updated_at`) VALUES
(1, 'samadi samir', '12 rue afrah fes', '2020-11-29 09:58:15', '2020-11-29 09:58:15');

-- --------------------------------------------------------

--
-- Structure de la table `tables`
--

CREATE TABLE `tables` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `slug` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `status` tinyint(1) NOT NULL DEFAULT '1',
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

--
-- Déchargement des données de la table `tables`
--

INSERT INTO `tables` (`id`, `name`, `slug`, `status`, `created_at`, `updated_at`) VALUES
(1, 'table 1', 'table-1', 1, '2020-11-29 08:49:19', '2020-11-29 08:49:19'),
(2, 'table 2', 'table-2', 0, '2020-11-29 08:49:33', '2020-12-03 09:21:45'),
(3, 'table 3', 'table-3', 0, '2020-11-29 08:49:53', '2020-11-30 10:39:40'),
(4, 'table 4', 'table-4', 0, '2020-11-29 08:50:07', '2020-11-30 10:40:45'),
(5, 'table 5', 'table-5', 0, '2020-11-29 08:50:23', '2020-11-29 11:42:56'),
(7, 'table 6', 'table-6', 0, '2020-11-29 12:05:11', '2020-11-30 08:44:09');

-- --------------------------------------------------------

--
-- Structure de la table `users`
--

CREATE TABLE `users` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `email` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
  `email_verified_at` timestamp NULL DEFAULT NULL,
  `password` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `remember_token` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

--
-- Déchargement des données de la table `users`
--

INSERT INTO `users` (`id`, `name`, `email`, `email_verified_at`, `password`, `remember_token`, `created_at`, `updated_at`) VALUES
(1, 'samadi samir', 'samadi@email.com', '2020-12-01 12:16:40', '$2y$10$iY7sWGNVA4K.06wG0DNdUuHaGbsltQXugJnWf5EoHX3mD.B6p9WXi', 'xHgwzX7oPmT3rLkE9J1yVbcw8DFCiO7ozFUV04yxwYL62gHgrBDip9MJN5FF', '2020-12-01 12:16:40', '2020-12-01 12:16:40');

--
-- Index pour les tables déchargées
--

--
-- Index pour la table `categories`
--
ALTER TABLE `categories`
  ADD PRIMARY KEY (`id`);

--
-- Index pour la table `failed_jobs`
--
ALTER TABLE `failed_jobs`
  ADD PRIMARY KEY (`id`);

--
-- Index pour la table `menus`
--
ALTER TABLE `menus`
  ADD PRIMARY KEY (`id`),
  ADD KEY `menus_category_id_foreign` (`category_id`);

--
-- Index pour la table `menu_sales`
--
ALTER TABLE `menu_sales`
  ADD PRIMARY KEY (`id`),
  ADD KEY `menu_sales_menu_id_foreign` (`menu_id`),
  ADD KEY `menu_sales_sales_id_foreign` (`sales_id`);

--
-- Index pour la table `migrations`
--
ALTER TABLE `migrations`
  ADD PRIMARY KEY (`id`);

--
-- Index pour la table `password_resets`
--
ALTER TABLE `password_resets`
  ADD KEY `password_resets_email_index` (`email`);

--
-- Index pour la table `sales`
--
ALTER TABLE `sales`
  ADD PRIMARY KEY (`id`),
  ADD KEY `sales_user_id_foreign` (`servant_id`);

--
-- Index pour la table `sales_table`
--
ALTER TABLE `sales_table`
  ADD PRIMARY KEY (`id`),
  ADD KEY `sales_table_sales_id_foreign` (`sales_id`),
  ADD KEY `sales_table_table_id_foreign` (`table_id`);

--
-- Index pour la table `servants`
--
ALTER TABLE `servants`
  ADD PRIMARY KEY (`id`);

--
-- Index pour la table `tables`
--
ALTER TABLE `tables`
  ADD PRIMARY KEY (`id`);

--
-- Index pour la table `users`
--
ALTER TABLE `users`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `users_email_unique` (`email`);

--
-- AUTO_INCREMENT pour les tables déchargées
--

--
-- AUTO_INCREMENT pour la table `categories`
--
ALTER TABLE `categories`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=8;

--
-- AUTO_INCREMENT pour la table `failed_jobs`
--
ALTER TABLE `failed_jobs`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT pour la table `menus`
--
ALTER TABLE `menus`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=10;

--
-- AUTO_INCREMENT pour la table `menu_sales`
--
ALTER TABLE `menu_sales`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=18;

--
-- AUTO_INCREMENT pour la table `migrations`
--
ALTER TABLE `migrations`
  MODIFY `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=16;

--
-- AUTO_INCREMENT pour la table `sales`
--
ALTER TABLE `sales`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=26;

--
-- AUTO_INCREMENT pour la table `sales_table`
--
ALTER TABLE `sales_table`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=14;

--
-- AUTO_INCREMENT pour la table `servants`
--
ALTER TABLE `servants`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=2;

--
-- AUTO_INCREMENT pour la table `tables`
--
ALTER TABLE `tables`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=8;

--
-- AUTO_INCREMENT pour la table `users`
--
ALTER TABLE `users`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=2;

--
-- Contraintes pour les tables déchargées
--

--
-- Contraintes pour la table `menus`
--
ALTER TABLE `menus`
  ADD CONSTRAINT `menus_category_id_foreign` FOREIGN KEY (`category_id`) REFERENCES `categories` (`id`) ON DELETE CASCADE;

--
-- Contraintes pour la table `menu_sales`
--
ALTER TABLE `menu_sales`
  ADD CONSTRAINT `menu_sales_menu_id_foreign` FOREIGN KEY (`menu_id`) REFERENCES `menus` (`id`) ON DELETE CASCADE,
  ADD CONSTRAINT `menu_sales_sales_id_foreign` FOREIGN KEY (`sales_id`) REFERENCES `sales` (`id`) ON DELETE CASCADE;

--
-- Contraintes pour la table `sales`
--
ALTER TABLE `sales`
  ADD CONSTRAINT `sales_user_id_foreign` FOREIGN KEY (`servant_id`) REFERENCES `users` (`id`) ON DELETE CASCADE;

--
-- Contraintes pour la table `sales_table`
--
ALTER TABLE `sales_table`
  ADD CONSTRAINT `sales_table_sales_id_foreign` FOREIGN KEY (`sales_id`) REFERENCES `sales` (`id`) ON DELETE CASCADE,
  ADD CONSTRAINT `sales_table_table_id_foreign` FOREIGN KEY (`table_id`) REFERENCES `tables` (`id`) ON DELETE CASCADE;
COMMIT;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
                                    
                                

2- إضافة ل Category Model


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

                                    
                                        <?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
    //
    protected $fillable = ["title", "slug"];

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

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

3- إضافة ل CategoryController


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

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

                                      
                                        <?php

namespace App\Http\Controllers;

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

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

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

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

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

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

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Category  $category
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Category $category)
    {
        //validation
        $this->validate($request, [
            "title" => "required|min:3"
        ]);
        //store data
        $title = $request->title;
        $category->update([
            "title" => $title,
            "slug" => Str::slug($title)
        ]);
        //redirect user
        return redirect()->route("categories.index")->with([
            "success" => "catégorie modifiée avec succés"
        ]);
    }

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

4- إضافة ل Menu Model


زيد model جديد سميه Menu فيه غادي تكون la relation مع ل category و sales لي هما les ventes وغادي نزيدوهم من بعد.

وغادي نبدلوا ل option ديال recherche من ل id ل slug.

الكود لي غادي تزيد فل model Menu هو :

                                        
                                            <?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Menu extends Model
{
    //
    protected $fillable = ["title", "slug", "description", "image", "price", "category_id"];

    public function category()
    {
        return $this->belongsTo(Category::class);
    }

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

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

5- إضافة ل MenuController


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

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

                                        
                                            <?php

namespace App\Http\Controllers;

use App\Menu;
use App\Category;
use Illuminate\Support\Str;
use Illuminate\Http\Request;

class MenuController extends Controller
{

    public function __construct()
    {
        $this->middleware("auth");
    }

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

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
        return view("managments.menus.create")->with([
            "categories" => Category::all()
        ]);
    }

    /**
     * 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, [
            "title" => "required|min:3|unique:menus,title",
            "description" => "required|min:5",
            "image" => "required|image|mimes:png,jpg,jpeg|max:2048",
            "price" => "required|numeric",
            "category_id" => "required|numeric",
        ]);
        //store data
        if ($request->hasFile("image")) {
            $file = $request->image;
            $imageName = time() . "_" . $file->getClientOriginalName();
            $file->move(public_path('images/menus'), $imageName);
            $title = $request->title;
            Menu::create([
                "title" => $title,
                "slug" => Str::slug($title),
                "description" =>  $request->description,
                "price" =>  $request->price,
                "category_id" =>  $request->category_id,
                "image" =>  $imageName,
            ]);
            //redirect user
            return redirect()->route("menus.index")->with([
                "success" => "menu ajouté avec succés"
            ]);
        }
    }

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

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Menu  $menu
     * @return \Illuminate\Http\Response
     */
    public function edit(Menu $menu)
    {
        //
        return view("managments.menus.edit")->with([
            "categories" => Category::all(),
            "menu" => $menu
        ]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Menu  $menu
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Menu $menu)
    {
        //
        //validation
        $this->validate($request, [
            "title" => "required|min:3|unique:menus,title," . $menu->id,
            "description" => "required|min:5",
            "image" => "image|mimes:png,jpg,jpeg|max:2048",
            "price" => "required|numeric",
            "category_id" => "required|numeric",
        ]);
        //store data
        if ($request->hasFile("image")) {
            unlink(public_path('images/menus/' . $menu->image));
            $file = $request->image;
            $imageName = time() . "_" . $file->getClientOriginalName();
            $file->move(public_path('images/menus'), $imageName);
            $title = $request->title;
            Menu::create([
                "title" => $title,
                "slug" => Str::slug($title),
                "description" =>  $request->description,
                "price" =>  $request->price,
                "category_id" =>  $request->category_id,
                "image" =>  $imageName,
            ]);
            //redirect user
            return redirect()->route("menus.index")->with([
                "success" => "menu modifié avec succés"
            ]);
        } else {
            $title = $request->title;
            $menu->update([
                "title" => $title,
                "slug" => Str::slug($title),
                "description" =>  $request->description,
                "price" =>  $request->price,
                "category_id" =>  $request->category_id
            ]);
            //redirect user
            return redirect()->route("menus.index")->with([
                "success" => "menu modifié avec succés"
            ]);
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Menu  $menu
     * @return \Illuminate\Http\Response
     */
    public function destroy(Menu $menu)
    {
        //remove image
        unlink(public_path('images/menus/' . $menu->image));
        $menu->delete();
        //redirect user
        return redirect()->route("menus.index")->with([
            "success" => "menu supprimé avec succés"
        ]);
    }
}
                                        
                                    

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