mardi 10 octobre 2017

PHP: Undefined offset: 0, but not with dd()

I am an Undefined offset: 0 error with laravel. The strange thing is that when I dd() it, it shows up.

  $ports = $container->getPorts();
  $ports = $ports[0];

This returns Undefined offset: 0

When I dd()

$ports = $container->getPorts();
dd($ports[0]);

Port {#336 
  #privatePort: 80
  #publicPort: 32780
  #type: "tcp"
}

I am getting this error with the docker-php sdk. Anyone that has a clue about what is going on?

Full code:

public function dockerContainers()
{
    $docker = new \Docker\Docker();
    $containerManager = $docker->getContainerManager();
    $containers = $containerManager->findAll();

    $data = [];
    $x=0;
    foreach ($containers as $container) {
        $ports = $container->getPorts();
        // dd($ports[0]);
        $ports = $ports[0];
        $privatePort = $ports->getPrivatePort();
        $publicPort = $ports->getPublicPort();
        $data[$x++] = [
            'id'    => $container->getId(),
            'state'  => $container->getState(),
            'names'  => $container->getNames(),
            'image'  => $container->getImage(),
            'status' => $container->getStatus(),
            'ports'  => [
                'privatePort' => $privatePort,
                'publicPort'  => $publicPort
            ]
        ];
    }

    return view('containers', [ 'containers' => $data ]);
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire