【问题标题】:Laravel bind to tables with belongsToLaravel 使用 belongsTo 绑定到表
【发布时间】:2015-07-17 14:08:40
【问题描述】:

我想绑定到具有关系的表。一张桌子是posts,第二张桌子是users。在帖子表中有user_id

我该怎么做?

【问题讨论】:

  • 请添加代码到目前为止你做了什么??
  • 你不能写这样的问题,我们需要更多信息

标签: laravel-4 model-binding


【解决方案1】:

在您的帖子模型上

public function user()
{
    return $this->belongsTo('User');
}

如果你需要反向关系,在你的用户模型上

public function posts()
{
    return $this->hasMany('Post');
}

【讨论】:

    猜你喜欢
    • 2021-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多