Support the ongoing development of Laravel.io →
API Laravel

I'm a novice of Laravel. I'm trying to make a CURL request to another endpoint in my Laravel application, but when I click the search button, the browser keeps loading and doesn't show anything. I've tried adding dd($result) and dd($inventory) to my code to debug the issue, but nothing is being printed.

Here's the relevant code in my routes/api.php:

Route::get('/api/client/{category}', [InventoryApiController::class, 'searchByCategory']);

Here's the relevant code in my routes/web.php:

Route::get('/client', function (){
    return view('inventory.client');
});

Route::post('/client', [InventoryApiController::class, 'search']);

Here's the relevant code in my InventoryApiController.php file:

public function search(Request $request){
    $category = $request->input('category');
    $url = "http://127.0.0.1:8000/api/client/$category";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $result = curl_exec($ch);
    if ($result === false) {
        $error = curl_error($ch);
        curl_close($ch);
        return view('inventory.client')->with('error', $error);
    }
    curl_close($ch);
    $inventory = json_decode($result);
    return view('inventory.client', compact('inventory'));
}

Here's the relevant code in my client.blade.php file:

<form method="POST" action="{{ url('/client') }}">
    @csrf
    <input type="text" name="category" placeholder="Enter category">
    <button type="submit">Search</button>
</form>

@if(isset($inventory))
    <h2>Search Results:</h2>
    <ul>
        @foreach($inventory as $item)
            <li>{{ $item->name }} - {{ $item->description }} - {{ $item->image }} - {{ $item->quantity }} - {{ $item->price }}</li>
        @endforeach
    </ul>
@else
    <p>No results found.</p>
@endif

I'm not sure what's causing this issue or how to debug it further. Any help would be greatly appreciated. Thank you!

Last updated by @tsy-0730 2 years ago.
0

Sign in to participate in this thread!

PHPverse

Your banner here too?

Yang tsy-0730 Joined 17 Apr 2023

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.