From e21fa78f15f3c4ed6a653418d6b6038e051cbdc8 Mon Sep 17 00:00:00 2001 From: bad Date: Wed, 15 Dec 2021 18:57:03 +0100 Subject: [PATCH] Display and search categories --- app/Http/Controllers/CartController.php | 10 ++++------ app/Http/Controllers/MainPageController.php | 19 ++++++++++++------- resources/views/product/view.blade.php | 6 ++++++ 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/app/Http/Controllers/CartController.php b/app/Http/Controllers/CartController.php index 2033b45..c423185 100644 --- a/app/Http/Controllers/CartController.php +++ b/app/Http/Controllers/CartController.php @@ -33,13 +33,11 @@ class CartController extends Controller $user->cart()->syncWithoutDetaching([$product->uuid]); $quantity = $request->validate([ 'quantity' => 'numeric|min:1' - ])["quantity"]; + ])["quantity"] ?? 1; - if($quantity) { - $pivot = $user->cart()->findOrFail($product->uuid)->pivot; - $pivot->quantity = $quantity; - $pivot->save(); - } + $pivot = $user->cart()->findOrFail($product->uuid)->pivot; + $pivot->quantity = $quantity; + $pivot->save(); return back(); } diff --git a/app/Http/Controllers/MainPageController.php b/app/Http/Controllers/MainPageController.php index 46ad2a3..0758504 100644 --- a/app/Http/Controllers/MainPageController.php +++ b/app/Http/Controllers/MainPageController.php @@ -2,6 +2,7 @@ namespace App\Http\Controllers; +use App\Models\Category; use App\Models\Product; use Illuminate\Http\Request; @@ -9,13 +10,17 @@ class MainPageController extends Controller { public function index(Request $request) { - $query = $request->query->get("q"); - if ($query) { + $query = Product::query()->latest()->limit(100); + $search = $request->query->get("q"); + if ($search) { // O(n) query at best, malicious users can just insert % and _ characters into the query if they wanna, but it's fine half the class left every single field vulnurable to sqli so I don't wanna bother doing this properly with a full text search - $products = Product::query()->where('name', 'like', "%{$query}%")->orWhere('description', 'like', "?")->latest()->limit(100)->get(); - } else { - $products = Product::query()->latest()->limit(100)->get(); - } - return view("index", ["products" => $products, "query" => $query]); + $query = $query->where('name', 'like', "%{$search}%")->orWhere('description', 'like', "%{$search}%"); + } + $category = $request->query->get("category"); + if ($category) { + // O(n) query at best, malicious users can just insert % and _ characters into the query if they wanna, but it's fine half the class left every single field vulnurable to sqli so I don't wanna bother doing this properly with a full text search + $query = $query->whereHas('categories', fn ($b) => $b->where("name","=",$category)); + } + return view("index", ["products" => $query->get(), "query" => $search]); } } diff --git a/resources/views/product/view.blade.php b/resources/views/product/view.blade.php index 6647e35..33de0b7 100644 --- a/resources/views/product/view.blade.php +++ b/resources/views/product/view.blade.php @@ -18,6 +18,12 @@

{{ $product->name }}

{{ ($product->price) }}

+
+ @foreach($product->categories as $category) + $category->name]) }}"> {{ $category->name }} + @endforeach +
+
@auth @if(!Auth::user()->cart()->find($product))