From 6a84d001e6b27ff9d42663c3bd5eabd10565a848 Mon Sep 17 00:00:00 2001 From: AJ Date: Wed, 20 Dec 2023 02:24:54 -0600 Subject: [PATCH] admin product controller successfully tested in browser --- .../Admin/AdminProductController.php | 29 ++++++++++++++++++- zzAJ/notes.txt | 9 +++++- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/app/Http/Controllers/Admin/AdminProductController.php b/app/Http/Controllers/Admin/AdminProductController.php index d22bf34..c245cd8 100644 --- a/app/Http/Controllers/Admin/AdminProductController.php +++ b/app/Http/Controllers/Admin/AdminProductController.php @@ -20,7 +20,34 @@ public function index() { return view("admin.product.index")->with("viewData", $viewData); } - + + + public function store(Request $request) { + $request->validate([ + "name" => "required|max:255", + "description" => "required", + "price" => "required|numeric|gt:0", + "image" => 'image', + + ]); + + + // new products + $newProduct = new Product(); + $newProduct -> setName($request->input('name')); + $newProduct -> setDescription($request->input('description')); + $newProduct -> setPrice($request->input('price')); + $newProduct -> setImage("game.png"); + $newProduct -> save(); + + return back(); + + + + + // ========================== + } + // ========================== } diff --git a/zzAJ/notes.txt b/zzAJ/notes.txt index ee6afc9..f4e6dc0 100644 --- a/zzAJ/notes.txt +++ b/zzAJ/notes.txt @@ -143,6 +143,13 @@ Chapter 16 - List Products in Admin Panel Chapter 17 - Create Products - focus on the admin panel system to create products --/admin/products/store route will collect and store the newly created products data using a post HTTP request using a form with the controller method +- /admin/products/store route will collect and store the newly created products data using a post HTTP request using a form with the controller method +- in the AdminProductController.php file we create use a store function to receive a $request object which allows us to interact with the HTTP requeast handled by our app. This allows us to receive the inputs, cookies, and files from the request +- then we use the validate method provided by the $request object. If the validation rules pass it keeps going, otherwise it will throw an Exception +- TODO: check out the available validation rules in the docs +- then we create a newProduct instance and set the newProduct attributes based on values collected from the form. +- then, we invoke the save method, which inserts the object into the database +- NOTE: you can also use the HTML required attribute on the form for fields you need, but we don't need that since we're using Laravel's validations +