Add search

This commit is contained in:
bad 2021-12-05 19:22:00 +01:00
parent f736882f5e
commit a51c2e5159
3 changed files with 20 additions and 7 deletions

View file

@ -7,9 +7,15 @@ use Illuminate\Http\Request;
class MainPageController extends Controller class MainPageController extends Controller
{ {
public function index() public function index(Request $request)
{ {
$products = Product::query()->latest()->limit(100)->get(); $query = $request->query->get("q");
return view("index", ["products" => $products]); if ($query) {
// 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]);
} }
} }

View file

@ -32,10 +32,12 @@ nav {
padding: 1em; padding: 1em;
} }
nav .fa { .form-search {
font-size: 4vh; display:flex;
justify-content: center;
} }
main { main {
display: flex; display: flex;
flex-direction: column; flex-direction: column;

View file

@ -10,10 +10,15 @@
<body> <body>
<nav> <nav>
<i class="fa fa-hamburger"></i> <a href="{{ route("main") }}"> <i class="fa fa-hamburger"></i> </a>
<form action="{{ route("main") }}" method="GET" class="search-form">
<input type="text" name="q" value="{{ $query ?? "" }}">
<button type="submit"><i class="fa fa-search"></i></button>
</form>
<div> <div>
@auth @auth
<a href="{{ url("user") }}"> Hello {{ Auth::user()->name }} </a> <a href="{{ route("user") }}"> Hello {{ Auth::user()->name }} </a>
@endauth @endauth
@guest @guest
<a href="{{ route("login") }}">Login</a> <a href="{{ route("login") }}">Login</a>