Search code examples
htmllaravelformslaravel-8getmethod

How to make my API method GET through id connect with my html form on view - Laravel 8


Hi guys! Hope you can help me out with this: - So i have this code in my API controller: API controller

-This route in my API route: API route

-And this on my view (only the part im having problems with): view

ps: the line of form action is correctly seperated, where it says ..."get"action... , i just put it like that here because of space. Q.: When i use the route to GET all the data from the database my output is a json file with everything in it, but when i use this route to only get the data of one movie (in my case it's a DB of movies) the output is an empty JSON file, even tho the method works on postman. Can you help me out ? Thank you MY OUTPUT , of the empty json file


Solution

  • Using a form with getting method the form data will be appended in Query Parameter.

    /search/titulo -> it is wrong. the titulo word is not dynamic keyword.

    You get your dynamic search keyword in titulo keyword after ?titulo=avenger

    Use the request function to get the query parameter value

    $seach = request()->get('titulo');
    

    You should change your search to get a route to only /search?keyword=avenger like this.

    Full example

    In your blade file

    <form action"{{ route('search') }}" method="get">
         <input name="titulo">
         <button type="submit">Search</button>
    </form>
    

    In your route file

    Route::get('/search', [YourControllerName::class, 'methodname'])->name('search');
    

    In your controller File: Where you handle the searched keyword

    public function search() {
         $search = request()->get('titulo'); // Get the searched keyword value
         $result = ModelName::where('titulo', 'like', '%'.$search.'%')->get();
         return $result;
    }