Add search
This commit is contained in:
parent
f736882f5e
commit
a51c2e5159
3 changed files with 20 additions and 7 deletions
|
@ -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]);
|
||||
}
|
||||
}
|
||||
|
|
6
resources/scss/app.scss
vendored
6
resources/scss/app.scss
vendored
|
@ -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;
|
||||
|
|
|
@ -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>
|
||||
|
|
Loading…
Reference in a new issue