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
{
public function index()
public function index(Request $request)
{
$products = Product::query()->latest()->limit(100)->get();
return view("index", ["products" => $products]);
$query = $request->query->get("q");
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;
}
nav .fa {
font-size: 4vh;
.form-search {
display:flex;
justify-content: center;
}
main {
display: flex;
flex-direction: column;

View File

@ -10,10 +10,15 @@
<body>
<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>
@auth
<a href="{{ url("user") }}"> Hello {{ Auth::user()->name }} </a>
<a href="{{ route("user") }}"> Hello {{ Auth::user()->name }} </a>
@endauth
@guest
<a href="{{ route("login") }}">Login</a>