1

我正在使用 Laravel 4,如果用户已登录,我正在尝试传递信息。如果用户未登录,我想以纯文本形式显示其他内容

图书控制器

public function showIndex() { 

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

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

我尝试在 $user_information 变量上方添加 isset() 但收到此错误消息

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

索引刀片.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

在此处为名字添加了 isset,因为如果他们访问他们的帐户,他们必须提供名字。

我尝试按如下方式添加isset:

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

我当然尝试使用推荐的语法,但又一次得到“尝试获取非对象的属性”错误

4

1 回答 1

4

您需要先检查用户是否已登录:

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));
}

然后你就:

@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
于 2013-09-03T02:06:19.617 回答