Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

katawura's avatar

Pulling data from an API in laravel

l am trying to do a simple request using the buzzle packgae manager. However it's not working as l expected.

In my controller l have

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Product;
use App\Http\Requests;
use GuzzleHttp\Client;
use GuzzleHttp\Message\Request;
use GuzzleHttp\Message\Response;



class products extends Controller
{
    //
    public function index()
    {
        $client = new Client();
        $response = $client->get('https://api.envato.com/v1/discovery/search/search/item?site=themeforest.net&category=wordpress&sort_by=relevance&access_token=TOKEN');
        retrun $response;
        return view('products/show',compact('response'));
    }

}

l am then pulling this response into my views like this

@extends('layout')

@section('title', 'Products')

@section('content')

    <div class="panel panel-default">
        <div class="panel-heading"><h1>Products</h1></div>
        <div class="panel-body">
            <div class="row">
                @foreach ($response as $product)
                        <div class="col-sm-6 col-md-4">
                            <div class="thumbnail">
                               
                                <div class="caption">
                                    <h3>{{ $product->name }}</h3>
                                    
                                </div>
                            </div>
                        </div>
                @endforeach
            </div>
        </div>

        </div>
    </div>

@endsection

However this doesn't seem to work. Any ideas what l am doing wrong guys?

0 likes
7 replies
ohffs's avatar

Do you really have two return statements in your index() function?

mahmoud_eid's avatar

you don't need to return $response in index method just first line, and in your view you need to parse json first then create loop.

1 like
Qlic's avatar

Also, your script should fail due to your "retrun $response;" rule, since retrun is mispelled.

samo's avatar

Besides all of this, @katawura please remove your API key from your first post. Everyone with this code can access your private data AND can also download your purchased stuff with the API and your token.

Maybe @JeffreyWay can update the post faster?

Please or to participate in this conversation.