I have a searching bar in my learn project. I include it to the navbar.blade.php and when test it i become that url construction.
localhost:8890/home?_token=7dKh256Me65Wb0rCvlJWkkk1SKdGrpaRPFCHP7MW&_method=GET&search=test
but the controller isn´t calling. I need this url to make it work
localhost:8890/search?_token=7dKh256Me65Wb0rCvlJWkkk1SKdGrpaRPFCHP7MW&_method=GET&search=test
My route:
Route::get('search', [App\Http\Controllers\ProductController::class, 'search'])->name('search');
ProductController:
public function search(Request $request)
{
//Get the search vale from the request
$search = $request->input('search');
$products = Product::query()
->where('ean', 'LIKE', "%{$search}%")
->orWhere('manufacturer', 'LIKE', "%{$search}%")
->orWhere('name', 'LIKE', "%{$search}%")
->orWhere('productNumber', 'LIKE', "%{$search}%")
->orWhere('propertie', 'LIKE', "%{$search}%")
->get();
return view('products.search',compact('products'));
}
included navbar-search.blade.php
<!-- Right navbar links -->
<ul >
<!-- Navbar Search -->
<li >
<a data-widget="navbar-search" role="button">
<i ></i>
</a>
<div >
<form >
@csrf
<div >
<input type="text" name="search" placeholder="Search" required>
<div >
<form action="{{route('search')}}" method="GET">
@csrf
@method('GET')
<button type="submit">
<i ></i>
</button>
</form>
<button type="button" data-widget="navbar-search">
<i ></i>
</button>
</div>
</div>
</form>
</div>
</li>
</ul>
CodePudding user response:
As said by Sharma in the comments before, you shouldn't use 2 form nested. Controller isn´t calling because first form doesn't set action. You can edit it like this:
<ul >
<!-- Navbar Search -->
<li >
<a data-widget="navbar-search" role="button">
<i ></i>
</a>
<div >
<form action="{{route('search')}}" method="GET">
<div >
<input type="text" name="search" placeholder="Search" required>
<div >
<button type="submit">
<i ></i>
</button>
<button type="button" data-widget="navbar-search">
<i ></i>
</button>
</div>
</div>
</form>
</div>
</li>
</ul>
CodePudding user response:
Why did you create 2 forms in blade file and don't need to add @csrf and @method directive in get request
