Lynx
Lynx

Reputation: 1482

trying to get property of a non object in Laravel 4

I am using Laravel 4 and I am trying to pass information if the user IS logged in. If the user is not logged in I want to display something else in plain text

BookController

public function showIndex() { 

    $user_information = UsersInformation::find(Auth::user()->id);

    return View::make('book.index', array('pageTitle' => 'Book your appointment',  'user_information' => $user_information));
}

I tried adding isset() right above the $user_information variable but got this error message

Cannot use isset() on the result of a function call 
   (you can use "null !== func()" instead)

Index.blade.php

        @if (isset($user_information->first_name))
           <p> You already have the 1 step complete let's move on to the second step!</p>
        @else
           <p>first step. let's create a login name and let's get to know you better</p>
        @endif

added the isset here for the first name because if they access their account they must provide a first name.

I attempted to add the isset as follows:

             if (isset(UsersInformation::find(Auth::user()->id))) {
                $user_information = UsersInformation::find(Auth::user()->id);
             }

I of course tried using the recommended syntax but then once again got the 'Trying to get property of a non object' error

Upvotes: 1

Views: 3332

Answers (1)

Antonio Carlos Ribeiro
Antonio Carlos Ribeiro

Reputation: 87719

You need to check if the user is logged in first:

public function showIndex() { 

    if (Auth::check())
    {
        $user_information = UsersInformation::find(Auth::user()->id);
    }
    else
    {
        $user_information = false;
    }

    return View::make('book.index', array('pageTitle' => 'Book your appointment',  'user_information' => $user_information));
}

And then you just:

@if ($user_information and $user_information->first_name)
   <p> You already have the 1 step complete let's move on to the second step!</p>
@else
   <p>first step. let's create a login name and let's get to know you better</p>
@endif

Upvotes: 4

Related Questions