【问题标题】:Laravel database errorLaravel 数据库错误
【发布时间】:2015-10-07 14:38:53
【问题描述】:

目前我正在使用 laravel 制作票务系统。 但我对数据库结构不太熟悉。

我已经制作了两张带有迁移的表。

调用:

门票, 评论

很明显,一张票可以有多个 cmets,但一条评论只能有一张票。所以这是一对多的关系。

在门票模型中,我声明了这一点:

class tickets extends Model
{
    protected $fillable = ['title', 'content', 'slug', 'status', 'user_id'];

    public function comments()
    {
      return $this->hasMany('App\comments', 'post_id');
    }
}

在 Comments 模型中,我声明了这一点:

class comments extends Model
{
     protected $guarded = ['id'];
     public function ticket()
    {
        return $this->belongsTo('App\tickets');
    }
}

这是我的控制器:

 public function show($slug)
  {
    $ticket = tickets::whereSlug($slug)->firstOrFail();
    $comments = $ticket->comments()->get;
    return view('tickets.show',compact('ticket','comments'));
  }

执行此操作时,我收到错误:

TicketController.php 第 77 行中的 ErrorException:未定义属性: Illuminate\Database\Eloquent\Relations\HasMany::$get

我做错了什么?

谢谢。

【问题讨论】:

    标签: php laravel orm laravel-5 eloquent


    【解决方案1】:

    当我认为您的目的是调用 get() 方法时,您似乎正在尝试访问 get 属性。替换这个:

    $comments = $ticket->comments()->get;
    

    $comments = $ticket->comments()->get();
    

    【讨论】:

    • 哇不错!认真地看了几个小时这个问题!谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-15
    • 2016-10-07
    • 2021-01-05
    相关资源
    最近更新 更多