【问题标题】:trying to get property of a non object in Laravel 4试图在 Laravel 4 中获取非对象的属性
【发布时间】:2013-09-06 02:48:13
【问题描述】:

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

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

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

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

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

我尝试如下添加isset:

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

我当然尝试过使用推荐的语法,但随后再次出现“尝试获取非对象的属性”错误

【问题讨论】:

  • 向我们展示您使用isset() 的实际代码,以及任何其他相关代码。
  • 我编辑了我的问题。谢谢!

标签: php laravel laravel-4 isset


【解决方案1】:

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

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

【讨论】:

  • 看起来是这样做的。谢谢!
猜你喜欢
  • 2014-05-05
  • 1970-01-01
  • 2015-08-19
  • 2018-02-21
  • 2015-09-22
  • 2017-03-20
  • 2014-03-25
  • 2015-01-25
相关资源
最近更新 更多