简体   繁体   中英

Impossible to access an attribute (“nom”) on a null variable

I would like for each of my children, their picture appears but I have this error message that appears :

Impossible to access an attribute ("nom") on a null variable.

In my twig:

{% for eleve in eleves %}
  <div><img src="{{ asset('img/')~eleve.image.nom}}" alt="" width="200" height="200"></div> 
  <div>{{eleve.prenom}}</div>                             
{% endfor %}

In my controller :

/**
 * @Route("/accueilPrincipal", name="accueilPrincipal")
*/
public function eleve(Request $request, EntityManagerInterface $manager)
{
    // On récupere tous les eleves
    $repo = $this->getDoctrine()->getRepository(Image::class);
    $repo2 = $this->getDoctrine()->getRepository(Eleve::class);
    $images = $repo->findAll();
    $test = $repo2->findAll();

    $sexe = $this->getUser()->getSexe();
    $eleves = $this->getUser()->getElevestuteur1();
    $eleves2 = $this->getUser()->getElevestuteur2();

    $user = $this->getUser()->getNom();

    return $this->render('profile/accueilPrincipal.html.twig', [
        'eleves' => $eleves,
        'eleves2' => $eleves2,
        'user' => $user,
        'sexe' => $sexe,
        'images' => $images,
        'test' => $test,
    ]);
}

Examine the $eleves variable, and it should be a lot clearer. dump($eleves);die; before the return $this->render(...) will show you what is actually there, instead of what you think must be there, but you don't quite know.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM