【问题标题】:Trying to get property of non-object [laravel 5.2]试图获取非对象的属性 [laravel 5.2]
【发布时间】:2016-12-23 10:25:09
【问题描述】:

项目版本 5.2

我是 Laravel 5 的新手。请解决...

错误r:试图获取非对象的属性

Comment.php [型号]

namespace App;

use Illuminate\Database\Eloquent\Model;

class Comment extends Model
{
    //
    public function articals()
    {
        return $this->belongsTo('App\Artical');
    }

    protected $fillable = array('body','artical_id');


}

Article.php [型号]

namespace App;

use Illuminate\Database\Eloquent\Model;

class Artical extends Model
{
    //
    public function comments()
    {
        return $this->hasMany('App\Comment');
    }

    protected $fillable = array('title','body');


}

route.php [路由]

 use App\Artical;
 use App\Comment;

Route::get('/', function ()
{

 $c = Artical::find(18)->comments;
  foreach($c as $comment) 
  {
    print_r($comment->body.'<br />'); 
  }
}); // working ok.....but

  $a = Comment::find(18)->articals;
  print_r($a->title); // error:Trying to get property of non-object 




}

得到错误:试图获取非对象的属性

请帮帮我...

article table structure

comment table structure

【问题讨论】:

  • 显示错误的哪一行?
  • 它告诉你什么文件会引发错误?
  • 表示$a不是对象,也就是说articles大概为null。 Article::comments() 应该是 hasMany(Comment::class)Comment::articles() 应该是 Comment::article()。修复关系可能意味着为评论返回一篇文章,这意味着 $a-&gt;title 将起作用
  • 检查Comment::find(18)是否存在
  • $a = Comment::fint(18); print_r($a); // 输出 Empty Not ANY GETTING ERROR but $a = Comment::fint(18)->articles; // 得到错误:试图获取非对象的属性

标签: php laravel


【解决方案1】:

我认为你们的关系可能已经结束。从代码猜测关系是多对多?如果是这样,那么这些关系应该是 belongsToMany。还要确保您定义了关系并且它在正确的模型上是反向的(取决于哪个具有外键)。

https://laravel.com/docs/5.2/eloquent-relationships#defining-relationships

【讨论】:

    【解决方案2】:

    问题在于您的articles() 模型与您的Comment 关系。

    belongsTo关系上没有指定外键时,它会根据关系方法的名称来构建外键名。在这种情况下,由于您的关系方法是articles(),因此它将查找字段articles_id

    您要么需要将 articles() 关系重命名为 article()(这是有道理的,因为只有一篇文章),要么需要在关系定义中指定键名。

    class Comment extends Model
    {
        // change the relationship name
        public function article()
        {
            return $this->belongsTo('App\Article');
        }
    
        // or, specify the key name
        public function articles()
        {
            return $this->belongsTo('App\Article', 'article_id');
        }
    }
    

    注意,这与关系的hasOne/hasMany 侧不同。它根据类的名称构建键名,因此无需更改 comments() 关系在 Article 模型上的定义方式。

    【讨论】:

    • @ShivBhargav 您的其他 cmets 显示 $a = Comment::fint(18); print_r($a); 为您提供空输出。这意味着您没有 ID 为 18 的评论。这也是个问题。
    • 我有 3 个 id 为 18 的 cmets 和 2 个 id 为 17 的 cmets 但在两个 id 相同的问题中......
    • @ShivBhargav 这没有意义。你的comments 表没有唯一ID吗?
    • 请检查...文章表::::[i.stack.imgur.com/PYGOc.png]评论表:::[i.stack.imgur.com/FaWYL.png]
    • @ShivBhargav 您的articals 表和artical_id 字段拼写错误。它们应该是articlesarticle_id
    【解决方案3】:

    更改以下行

    $a = Comment::find(18)->articles;
    

    $a = Comment::find(18);
    

    【讨论】:

    • $a = Comment::fint(18); print_r($a); // 输出 Empty Not ANY GETTING ERROR but $a = Comment::fint(18)->a‌​rticles; // 得到错误:试图获取非对象的属性
    • Comment::find(18)->articles 的输出是什么; ?
    猜你喜欢
    • 2018-01-15
    • 2016-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-25
    • 2015-09-22
    相关资源
    最近更新 更多