TuxxyDOS
TuxxyDOS

Reputation: 195

Laravel - Can't pass data through view

I try to send data from a controller to a view on Laravel 5.4.

My controller "PagesController.php" :

public function bird($id) {

    $infoBirdById = new InfoBird();
    $birdById = $infoBirdById->getBirdById($id);

    $infoHealthById = new InfoHealth();
    $healthById = $infoHealthById->getHealthById($id);

    $infoTransportByIdBird = new InfoTransport();
    $transportByIdBird = $infoTransportByIdBird->getTransportByIdBird($id);

    $infoUserByIdHealer = new InfoUser();
    $userByIdHealer = $infoUserByIdHealer->getUserByIdHealer($id);

    var_dump($userByIdHealer);
    //var_dump($birdById);
    //var_dump($healthById);
    //var_dump($transportByIdBird);

    return view('pages.bird', compact('birdById', 'healthById', 'userByIdHealer', 'transportByIdBird'));
}

An extract of the view "bird.blade.php" :

<div class="block block-name">
    <h2>{{ $userByIdHealer->prenom }} {{ $userByIdHealer->nom }}</h2>
</div>

Here is the error : Property [prenom] does not exist on this collection instance.

The resultant of the dump of userByIdhealer :

object(Illuminate\Support\Collection)[216]
  protected 'items' => 
    array (size=1)
      0 => 
        object(stdClass)[223]
          public 'id_biodiv_acteur' => int 3
          public 'nom' => string 'Rinaire' (length=7)
          public 'prenom' => string 'Vété' (length=6)
          public 'civilite' => string 'Homme' (length=5)
          public 'date_naissance' => string '2017-05-10' (length=10)
          ect...

So I don't understand why I have this error, because in the dump I can see that "prenom" is here...

Thanks for your help !

Upvotes: 1

Views: 295

Answers (1)

Exprator
Exprator

Reputation: 27503

{{ $userByIdHealer[0]->prenom }} {{ $userByIdHealer[0]->nom }}

you are getting it inside 0 array so use this or you can loop with foreach and use without 0

Upvotes: 1

Related Questions