1

my problem is pretty straightforward but i am unable to find a solution.

the following code

{{$proposition->User}}

it returns the following as it should do, nothing weird here.

{
 "id":"2",
 "telefoon":"",
 "mobiel":"0622222222",
 "adres_huisnmr":"4e Binnenvestgracht",
 "email":"rubend@gmail.com",
 "postcode":"",
 "stad":"leiden",
 "achternaam":"Morgenland",
 "bio":"",
 "created_at":"2014-12-15 14:35:38",
 "updated_at":"2014-12-15 14:35:38"
}

the following code however

{{$proposition->User->achternaam}}

generates the following error:

Trying to get property of non-object (View: /home/collective/projects/laravel/app/views/Account/received_propositions.blade.php)

so as far as i can tell, the ORM is correct. the variable that i am trying to display IS there as shown in the first example. but why does this not work?

i hope someone can help me because i am stuck on this (what should be a relatively easy problem) for hours.

EDIT

the following generates an error

    @foreach($jobs as $job)

     <li>De Job:  {{$job->titel}}, {{$job->prijs}} euro,  {{$job->beschrijving}}  **todo**more info</li>

     @foreach($job->Propositions as $proposition)

    <ul>
       <li>
    offerte:  {{$proposition->titel}} prijs: {{$proposition->prijs}}
   ------->>this line!      geplaatsts door : {{$proposition->User->email}}
        </li>           
    </ul>     

  @endforeach
    @endforeach

if i change it to the following by adding a if statement that checks if the User!=null it WILL work, but it will only for the first one in the loop. all the other ones it will be left blank. the ORM is solid.

who can help me? really breaking my head over this

    @foreach($jobs as $job)

     <li>De Job:  {{$job->titel}}, {{$job->prijs}} euro,  {{$job->beschrijving}}  **todo**more info</li>

     @foreach($job->Propositions as $proposition)

    <ul>
       <li>
    offerte:  {{$proposition->titel}} prijs: {{$proposition->prijs}}
               geplaatsts door :    @if($proposition->User)
                  {{$proposition->User->email}}
           @endif
        </li>           
    </ul>     

  @endforeach
    @endforeach



       <ul>

    </ul>
Vudew
  • 1,235
  • 2
  • 9
  • 8
  • You should add that you use that code in a foreach loop (likely), because the problem is that not every `$proposition` has related `user`, so you're trying to call `->achternaam` on `null`. So simply [check if related model exists](http://stackoverflow.com/a/23911985/784588) – Jarek Tkaczyk Dec 20 '14 at 21:20
  • it is still not working, i have deleted everything in the DB except i only have 1 item left, and everything is working. as i stated above. the ORM is correct! they all have a user i am 100% sure. – Vudew Dec 21 '14 at 12:20
  • Well, so the accepted answer worked for you? I doubt.. – Jarek Tkaczyk Dec 21 '14 at 13:22
  • Show how you fetch the data, because there's no way that it shows `user->email` for first user only, other than doing something wrong there OR having no data in the db. – Jarek Tkaczyk Dec 21 '14 at 14:57

1 Answers1

-1

Use this instead.

{{$proposition->User()->achternaam}}
Dave Kyalo
  • 39
  • 5
  • Please add a bit more information. OP might not know why your code is working and his / hers isn't. Such additional context will also help anyone else reading your answer. – Max Leske Dec 20 '14 at 17:08