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

ziben69's avatar

Laravel 5.8 | Json data from external API to controller/view

Hello guys, i have URL with data (Json). I am a beginner when it comes to API. I need to know where to start to display the obtained data in a blade view. Can someone show me the way?

0 likes
4 replies
dev-mike's avatar

which package are you using to get data (JSON ) from the URL in laravel?

dev-mike's avatar

@ziben69 it is as same as you do in web, for eg. //here I'm using guzzle(PACk), good for handling API

 public function apiData(){
	$jsonData = Http::post('YOUR_JSON_DATA_URL')->json() ;

	return view('view.name',compact('jsonData'));
}

it will pass data to your view

ziben69's avatar

@dev-mike

use GuzzleHttp\Client;
...
    public function index()
    {
        $jsonData = Client::get('PATH')->json() ;

        $noUsers = DB::table('users')->count();
        return view('home', [
            'jsonData' => $jsonData
        ]);
    }

Error: Non-static method GuzzleHttp\Client::get() should not be called statically

dev-mike's avatar
dev-mike
Best Answer
Level 1

@ziben69 In laravel5.8, instead of

$jsonData = Client::get('PATH')->json() ;

use

$client = new Client();
$jsonData = $client->get('PATH')->getBody()->getContents();

$noUsers = DB::table('users')->count();
        return view('home', [
            'jsonData' => $jsonData
]);	
1 like

Please or to participate in this conversation.