试图获得非物体的财产 - Laravel 5

时间:2015-09-09 01:24:35

标签: php laravel

我正在尝试在文章中回显用户名,我正在获取ErrorException: Trying to get property of non-object。我的代码:

模型

1. News

    class News extends Model
    {
      public function postedBy()
      {
         return $this->belongsTo('App\User');
      }
      protected $table = 'news';
      protected $fillable = ['newsContent', 'newsTitle', 'postedBy'];
    }

2. User

    class User extends Model implements AuthenticatableContract,
                                AuthorizableContract,
                                CanResetPasswordContract
    {
        use Authenticatable, Authorizable, CanResetPassword;

        protected $table = 'users';

        protected $fillable = ['name', 'email', 'password'];

        protected $hidden = ['password', 'remember_token'];

    }

模式

users

enter image description here

news

enter image description here

控制器

public function showArticle($slug)
    {
        $article = News::where('slug', $slug)->firstOrFail();
        return view('article', compact('article'));
    }

刀片

{{ $article->postedBy->name }}

当我尝试删除刀片{{ $article->postedBy }}中的名称时,它会输出id,但当我尝试添加 - >名称时,它会显示Trying to get property of non-object但我有一个字段我的表格中的nameUser模型。我错过了什么吗?

9 个答案:

答案 0 :(得分:52)

您的查询是返回数组还是对象?如果将其转储出去,您可能会发现它是一个数组,您只需要一个数组访问([])而不是对象访问( - >)。

答案 1 :(得分:22)

我通过使用Jimmy Zoto的答案并向我的belongsTo添加第二个参数来实现它。这是:

首先,正如Jimmy Zoto所建议的那样,我的代码是$article->poster->name$article->poster['name']。接下来是在我的belongsTo中添加第二个参数,从return $this->belongsTo('App\User');到返回$this->belongsTo('App\User', 'user_id');,其中user_id是我在新闻表中的外键。

感谢您的帮助!

答案 2 :(得分:5)

我在父类中实现了hasOne关系,定义了外键和本地键,它返回了一个对象,但子列必须作为数组访问。
$parent->child['column']
有点令人困惑。

答案 3 :(得分:4)

发生这种情况的原因(预期)

假设我们有2个表 users subscription

1个用户有1个订阅

在用户模型中,我们有

public function subscription()
{
    return $this->hasOne('App\Subscription','user_id');
}

我们可以按以下方式访问订阅详细信息

$users = User:all();

foreach($users as $user){
    echo $user->subscription;
}

如果任何用户没有订阅,则可能是这种情况。 订阅后,我们将无法再使用箭头功能,如下所示:

$user->subscription->abc   [this will not work] 
$user->subscription['abc']  [this will work]

,但如果用户已订阅

$user->subscription->abc   [this will work] 

注意:尝试放置一个if条件

if($user->subscription){
 return $user->subscription->abc;
}

答案 4 :(得分:2)

经过一段时间我们需要运行

 'php artisan passport:install --force 

再次生成一个键,这解决了我的问题,

答案 5 :(得分:0)

我也有这个问题。在相关的控制器(例如UserController)中添加以下类似的代码

$users = User::all();
return view('mytemplate.home.homeContent')->with('users',$users);

答案 6 :(得分:0)

Laravel optional()Helper可以解决此问题。 尝试使用此帮助程序,以便如果任何键没有值,则它不会返回错误

foreach ($sample_arr as $key => $value) {        
      $sample_data[] = array(       
        'client_phone' =>optional($users)->phone        
      );
    }
 print_r($sample_data);

答案 7 :(得分:0)

如果您使用或循环(forforeach等)或关系(one to manymany to many等),这可能意味着查询将返回一个null变量或一个null关系成员。

例如:在表格中,您可能要列出users及其roles

<table>
    <tr>
        <th>Name</th>
        <th>Role</th>
    </tr>
    @foreach ($users as $user)
    <tr>
        <td>{{ $user->name }}</td>
        <td>{{ $user->role->name }}</td>
    </tr>
    @endforeach
</table>

在上述情况下,如果甚至有一个没有角色的用户,您可能会收到此错误。您应将{{ $user->role->name }}替换为{{ !empty($user->role) ? $user->role->name:'' }},如下所示:

<table>
    <tr>
        <th>Name</th>
        <th>Role</th>
    </tr>
    @foreach ($users as $user)
    <tr>
        <td>{{ $user->name }}</td>
        <td>{{ !empty($user->role) ? $user->role->name:'' }}</td>
    </tr>
    @endforeach
</table>

答案 8 :(得分:0)

为我工作:

{{ !empty($user->role) ? $user->role->name:'' }}
相关问题