skip to Main Content

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:

https://github.com/Maneneco/EmailManager

2

Answers


  1. To fix your code you need to add the $val variable to the Route:

    Route::get('meusprocessos/processos/search/{val}', [AppHttpControllersMeusProcessosController::class, 'processosSearch'])->name('meusprocessos.processosSearch');
    

    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

    Route::post('meusprocessos/processos/search', [AppHttpControllersMeusProcessosController::class, 'processosSearch'])->name('meusprocessos.processosSearch');
    

    In your jQuery search:

    $("#processos").post("{!! route('meusprocessos.processosSearch') !!}", { search: val });
    

    Then in your controller:

    public function processosSearch(Request $request)
    {
        $val = $request->search;
        echo $val;
        $search = $val;
        $processos = Processo::where('name', 'like', '%' . $search . '%')
            ->where('funcionario_id', auth()->user()->id)
            ->get();
        $departamentos = Departamento::all();
    
        return view('meusprocessos.processosSearch')->with([
            'processos' => $processos,
            'departamentos' => $departamentos
        ]);
    }
    

    Of course don’t forget to use the Request in your controller, at the top:

    use IlluminateHttpRequest;
    

    If for any reason you cannot use a post request, you can still do it using a get request like so:

    Route:

    Route::get('meusprocessos/processos/search', [AppHttpControllersMeusProcessosController::class, 'processosSearch'])->name('meusprocessos.processosSearch');
    

    Controller:

    public function processosSearch(Request $request)
    {
        $val = $request->search;
        echo $val;
        $search = $val;
        $processos = Processo::where('name', 'like', '%' . $search . '%')
            ->where('funcionario_id', auth()->user()->id)
            ->get();
        $departamentos = Departamento::all();
    
        return view('meusprocessos.processosSearch')->with([
            'processos' => $processos,
            'departamentos' => $departamentos
        ]);
    }
    

    You can then use it in jquery like so:

    $("#processos").load("{!! route('meusprocessos.processosSearch') !!}"+ "?search=" + val)
    
    Login or Signup to reply.
  2. Try this way:

    route:

    Route::get('/meusprocessos/processos/Search/{search?}', [AppHttpControllersMeusProcessosController::class, 'processosSearch'])->name('meusprocessos.processosSearch');
    

    jquery:

       $("#search").on("keyup", function(e) {
                var val = $.trim(this.value);
                if (e.key === 'Backspace') { //Use this to include backspace on keyup
                    $("#processos").load("{!! route('meusprocessos.processosSearch') !!}/" + val )
                }
                $("#processos").load("{!! route('meusprocessos.processosSearch') !!}/" + val )
            });
    

    controller:

      public function processosSearch($search = null)
        {
            $departamentos = Departamento::all();
    
            if($search == null){ //in case there is nothing on the input
                $processos = Processo::all();
            }else{
                $processos = Processo::where('nome', 'like', '%' . $search . '%')
                    ->where('funcionario_id', auth()->user()->id)
                    ->get();
            }
    
            return view('meusprocessos.processosSearch')->with(['processos' => $processos,
                'departamentos' => $departamentos
            ]);
        }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search