Chron's avatar
Level 7

Trying to get property of non-object

I'm using barryvdh/laravel-dompdf. When I pass the data of the current user I get the said error.

Controller

public function viewAssessment() {
        $user = Auth::user()->customer;
        $pdf = \PDF::loadView('print', compact('user'));
        return $pdf->stream('invoice.pdf');

    }

View

<ul>
            @foreach($user as $u)
            <li>{{ $u->firstname }}</li>
            @endforeach
</ul>
0 likes
3 replies
nemrut's avatar
nemrut
Best Answer
Level 1

why you loop into single $user ? and are you trying to get firstname of customer or user? because you set costumer to the $user variable. So seems like your customer doesn't has a firstname attribute. try,

public function viewAssessment() {
        $user = Auth::user();
        $pdf = \PDF::loadView('print', compact('user'));
        return $pdf->stream('invoice.pdf');
}

and in view

<ul>
    <li>{{ $user->firstname }}</li>
    //or if trying to get user's customer's name
    <li>{{ $user->customer->firstname }}</li>
</ul>
Chron's avatar
Level 7

@NEMRUT - I'm trying it.. But it loads too slow..

Edit: Still having the same error.

SafeMood's avatar

@CHRON -

hi @chron

if u still having to same prob

just change this part

  • {{ $user->customer->firstname }}
  • to

  • {{ ($user->customer) ? $user->customer->firstname : "" }}
  • Please or to participate in this conversation.