I am doing a project on laravel, and I have a search input where I use jquery to search for the word.
But I getting the following error, but I don’t know what I am doing wrong.
GET http://127.0.0.1:8000/meusprocessos/processos/Search?search=word 500 (Internal Server Error)
Here resumed html:
@extends('adminlte::page')
@section('title', 'Dashboard')
@section('content')
<link rel="stylesheet" href="path/to/font-awesome/css/font-awesome.min.css">
<div class="col-md-9 mb-5">
<div class="box-tools pull-right">
<div class="has-feedback">
<form>
<input type="text" name="search" class="form-control input-sm" id="search" value="" placeholder="Procurar Processo pelo nome">
<!-- <a type="submit" class="btn btn-primary" id="processosSearch">Procurar</a> -->
</form>
</div>
</div>
<div id='processos'>
</div>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.6.2.min.js"
integrity="sha256-2krYZKh//PcchRtd+H+VyyQoZ/e3EcrkxhM8ycwASPA=" crossorigin="anonymous"></script>
<script>
$("#refreshProcessos").on('click', function () {
$("#processos").html("<img src=' https://flevix.com/wp-content/uploads/2019/07/Curve-Loading.gif' >")
$("#processos").load("{!! route('meusprocessos.processos') !!}")
});
$("#search").on("keyup", function() {
var val = $.trim(this.value);
$("#processos").load("{!! route('meusprocessos.processosSearch') !!}" + "?search=" + val )
});
$(document).on('click', 'a.readProcesso', function (e) {
var id = $(this).attr("data-id")
//console.log(id);
$("#processos").html("<img src=' https://flevix.com/wp-content/uploads/2019/07/Curve-Loading.gif' >")
$("#processos").load("{!! route('meusprocessos.processo') !!}" + "/" + id)
});
$("#processos").html("<img src=' https://flevix.com/wp-content/uploads/2019/07/Curve-Loading.gif' >")
$("#processos").load("{!! route('meusprocessos.processos') !!}")
</script>
@stop
Here the route:
Route::get('/meusprocessos/processos/Search', [AppHttpControllersMeusProcessosController::class, 'processosSearch'])->name('meusprocessos.processosSearch');
Here de controller:
public function processosSearch(Resquest $request)
{
$search = $request->val;
// $search = "Anulado";
$processos = Processo::where('nome', 'like', '%' . $search . '%')
->where('funcionario_id', auth()->user()->id)
->get();
$departamentos = Departamento::all();
return view('meusprocessos.processosSearch')->with(['processos' => $processos,
'departamentos' => $departamentos
]);
}
Its not going to the view at all, but I don’t know what I am doing wrong. Its problably the way I am applying the routes, but I don’t know how doing it in a diferent way.
I don’t think is needed the rest of the code, but its here:
2
Answers
To fix your code you need to add the $val variable to the Route:
With that being said I’d also like to recommend you to use post requests when you want to have a route for search, and instead of passing the search query in the url, you should pass it in the request body.
Something like this:
routes/web.php
In your jQuery search:
Then in your controller:
Of course don’t forget to use the Request in your controller, at the top:
If for any reason you cannot use a post request, you can still do it using a get request like so:
Route:
Controller:
You can then use it in jquery like so:
Try this way:
route:
jquery:
controller: