You set the url to search url:"search",
Yet your routes are different. Try this
url: "/admin/home/search",
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I'm following a tutorial to implement live search feature in my laravel project using ajax.
I'm following the tutorial and understand every stap he takes but I recieve an error 405 (Method not allowed). Can anyone see what I'm doing wrong?
Here's my code:
My view:
<body class="relative min-h-screen">
@include('partials.header')
<main class="md:w-3/5 m-auto pb-20 space-y-10">
<div class="p-2 mt-4 flex justify-center items-center">
<p class="w-10/12">Welkom Admin. Op deze pagina kan je al onze gebruikers terugvinden en heb je beheer over hun geboortelijst.
</p>
</div>
<div class="bg-mid p-4 mb-8 flex flex-row justify-between items-center ">
<h3 class=""><strong>Gebruikers</strong></h3>
</div>
<input type="search" name="search" id="search" class="border-2 rounded-lg p-2">
<div class="w-full bg-mid p-2" id="Table">
</div>
</main>
@include('partials.footer')
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js" integrity="sha512-+NqPlbbtM1QqiK8ZAo4Yrj2c4lNQoGv8P79DPtKzj++l5jnN39rHA/xsqn8zE9l0uSoxaCdrOgFs6yjyfbBxSg==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha512-ldc1sPu1FZ8smgkgp+HwnYyVb1eRn2wEmKrDg1JqPEb02+Ei4kNzDIQ0Uwh0AJVLQFjJoWwG+764x70zy5Tv4A==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
{{-- Script voor live searching --}}
<script>
$(document).ready(function(){
$('#search').on('keyup', function(){
const query = $(this).val();
$.ajax({
url:"search",
type: "GET",
data:{'search': query},
success: function(data){
$('$Table').html(data);
}
});
});
});
</script>
</body>
My routes:
Route::get('/admin/home', [AdminController::class, 'adminHome'])
->name('adminhome');
Route::get('/admin/home/search', [AdminController::class, 'search'])
->name('search');
My Controller:
public function search(Request $request)
{
$users = User::where('role', '=', 'user')->get();
if($request->ajax()) {
$data = $users->where('id', 'like', '%'.$request->match.'%')
->orwhere('name', 'like', '%'.$request->match. '%')
->orwhere('email', 'like', '%'.$request->match. '%')->get();
$output = '';
if (count($data) > 0) {
$output = `
<table>
<thead>
<tr>
<th scope="col">Id</th>
<th scope="col">Naam</th>
<th scope="col">E-mail</th>
</tr>
</thead>
<tbody>`;
foreach ($data as $row) {
$output .=`
<tr>
<th scope="row">`.$row->id.`</th>
<td>`.$row->name.`</td>
<td>`.$row->email.`</td>
</tr>
`;
}
$output .= `
</tbody>
</table>`;
}
} else {
$output = 'Geen gebruikers voldoen aan uw zoekcriteria..';
}
}
@stefverniers debug by just returning a simple string and if that works, move that html to a view file
Please or to participate in this conversation.