试图获得非物体的属性 - Laravel 5

时间:2022-05-15 15:42:17

I'm trying to echo out the name of the user in my article and I'm getting the ErrorException: Trying to get property of non-object. My codes:

我试图在我的文章中回显用户的名字,我得到ErrorException:尝试获取非对象的属性。我的代码:

Models

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'];

    }

Schema

table users

试图获得非物体的属性 -  Laravel 5

table news

试图获得非物体的属性 -  Laravel 5

Controller

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

Blade

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

When I try to remove name in the blade {{ $article->postedBy }} it outputs the id, but when I try to add the ->name there it says Trying to get property of non-object but I have a field name in my table and a User model. Am I missing something?

当我尝试删除刀片中的名称{{$ article-> postedBy}}时,它输出了id,但是当我尝试添加 - > name时,它说试图获取非对象的属性但是我有一个字段名称在我的表和用户模型中。我错过了什么吗?

4 个解决方案

#1


35  

Is your query returning array or object? If you dump it out, you might find that it's an array and all you need is an array access ([]) instead of an object access (->).

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

#2


13  

I got it working by using Jimmy Zoto's answer and adding a second parameter to my belongsTo. Here it is:

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

First, as suggested by Jimmy Zoto, my code in blade from $article->poster->name to $article->poster['name']. Next is to add a second parameter in my belongsTo, from return $this->belongsTo('App\User'); to return $this->belongsTo('App\User', 'user_id'); in which user_id is my foreign key in the news table.

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

Thanks for all your help!

感谢你的帮助!

#3


3  

I implemented a hasOne relation in my parent class, defined both the foreign and local key, it returned an object but the columns of the child must be accessed as an array.
i.e. $parent->child['column']
Kind of confusing.

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

#4


1  

It happen that after some time we need to run

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

 'php artisan passport:install --force 

again to generate a key this solved my problem ,

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

#1


35  

Is your query returning array or object? If you dump it out, you might find that it's an array and all you need is an array access ([]) instead of an object access (->).

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

#2


13  

I got it working by using Jimmy Zoto's answer and adding a second parameter to my belongsTo. Here it is:

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

First, as suggested by Jimmy Zoto, my code in blade from $article->poster->name to $article->poster['name']. Next is to add a second parameter in my belongsTo, from return $this->belongsTo('App\User'); to return $this->belongsTo('App\User', 'user_id'); in which user_id is my foreign key in the news table.

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

Thanks for all your help!

感谢你的帮助!

#3


3  

I implemented a hasOne relation in my parent class, defined both the foreign and local key, it returned an object but the columns of the child must be accessed as an array.
i.e. $parent->child['column']
Kind of confusing.

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

#4


1  

It happen that after some time we need to run

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

 'php artisan passport:install --force 

again to generate a key this solved my problem ,

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