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

Jakub003's avatar

Convert JsonResponse with json_decode not working?

When I dd I get this

I am trying to figure out how to access the content so I can do a for loop in the blade.

I tried to make this into a collection using

$array = collect(json_decode($result,true));

But when I dd this, I end up getting

null // app\Http\Livewire\Guest\Home\DisplayGoogleSheetsProducts.php:24

#GoogleSheetsSerivces

<?php

namespace App\Http\Services;

use Google\Client;
use Google\Service\Sheets;
use Google\Service\Sheets\ValueRange;

class GoogleSheetsServices
{
    public $client;
    public $service;
    public $documentId;
    public $range;

    public function __construct()
    {
        $this->client = $this->getClient();
        $this->service = new Sheets($this->client);
        $this->documentId = 'ssasdasdsd';
        $this->range = 'A:Z';
    }

    public function getClient()
    {
        $client = new Client();
        $client->setApplicationName('One Stop Repair Phones');
        $client->setRedirectUri('http://template-1stoprepair.test/');
        $client->setScopes(Sheets::SPREADSHEETS);
        $client->setAuthConfig('googleSheetsCredentials.json');
        $client->setAccessType('offline');

        return $client;
    }

    public function readSheet()
    {
        $doc = $this->service->spreadsheets_values->get($this->documentId,$this->range);

        return $doc;
    }


}
// inside livewire component
 public function mount(Request $request)
    {
        $data = (new GoogleSheetsServices ())->readSheet();

        $result = response()->json($data);

        $array = collect(json_decode($result,true));

    }

0 likes
6 replies
vincent15000's avatar

What contains the $result variable ? Can you show the complete code before $array = ... ?

1 like
Jakub003's avatar

@vincent15000

Updated the original post with the code

It has the json response

// inside livewire component
 public function mount(Request $request)
    {
        $data = (new GoogleSheetsServices ())->readSheet();

        $result = response()->json($data);

        $array = collect(json_decode($result,true));

    }
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

@Jakub003 Why are you making it into a json response if you want to use it in blade. Arent you returning a view ? If so, just send the $data to the view

Jakub003's avatar

@Sinnbeck Ooof I am such a newb lol, ty did not even occur to me I was googling for like an hour on how to read json in laravel haha

Jakub003's avatar

@Sinnbeck Sorry about that, added the code in the original post for the services and the mount method in livewire component

Please or to participate in this conversation.