Laravel – Variable is empty but is not empty? [duplicate]

  api, command, http, laravel, php

I create new own command in laravel, which updates some rows in mysql (in this case minecraft servers):

public function handle()
{
    $serversList = [
        'cubegame.pl',
        'dermc.pl',
        'feerko.pl',
        'skyup.pl',
        'mcosada.pl',
        'dragon-craft.pl',
        'gc2.pl'
    ];

    foreach ($serversList as $server) {
        $req = Http::get("https://api.mcsrvstat.us/2/$server");

        if($req->ok()) {
            $status = $req->json();

            echo( $status['motd']['html'][0] );

            // $serv = Server::updateOrCreate(
            //     [ 'name' => $server ],
            //     // [ 'name' => $server, 'ip' => $status['ip'] ],
            //     [
            //         'ip' => $status['ip'],
            //         'motd' => $status['motd']['html'][0] ?? null,
            //         'playersOnline' => $status['players']['online'] ?? null,
            //         'slots' => $status['players']['max'] ?? null,
            //         'version' => $status['version'] ?? null,
            //         'isOnline' => $status['online'],
            //         'icon' => $status['icon'],
            //     ]
            // );

        }
    }

    return 0;
}

When code was uncommented it throw error:

ErrorException Undefined index: motd.

Now, when in code is only echo it print variable $status['motd']['html'][0], but in next line it throws error

Undefined array key "motd"

Im so confused, please help.

Sorry for English :p

Source: Laravel

Leave a Reply