【问题标题】:Laravel 5: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'users.moderators'Laravel 5:SQLSTATE [42S22]:找不到列:1054 未知列“users.moderators”
【发布时间】:2016-01-06 16:16:38
【问题描述】:

我猜这是模型关系错误,但我似乎无法确定。

我有 4 张桌子

users: id, name, email...
subreddits: id, user_id...
posts: id, title, subreddit_id, user_id...
moderators: id, user_id, subreddit_id...

我正在尝试通过热切加载“user.votes”和“subreddit.moderators”来获取所有帖子

$post = Post::with('user.votes')->with('subreddit.moderators')->findOrFail($post->id);

但我不断收到此错误

Connection.php 第 636 行中的 QueryException:

SQLSTATE[42S22]:未找到列:1054 'where 子句'中的未知列'users.moderators'(SQL:select * from users where users.moderators in (17))

如果我单独使用subreddit 加载它,它会起作用,但我还需要获得 subreddit 的版主。

编辑:我从Subreddit 模型中的moderators() 方法中删除了moderators 字段,并将App\User 替换为App\Moderator,我现在可以获取相关的子reddit 及其版主。但是,访问$post->subreddit->moderators->user_id 给了我这个错误Undefined property: Illuminate\Database\Eloquent\Collection::$user_id

这些是我的模型

User模特

public function subreddit() {
    return $this->hasMany('App\Subreddit');
}

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

public function votes(){
    return $this->hasManyThrough('App\Vote','App\Post');
}

public function moderators(){
    return $this->hasManyThrough('App\Moderator', 'App\Subreddit');
}

Subreddit模特

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

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

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

Post模特

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

Moderator 型号

public function subreddit() {
    return $this->belongsTo('App\Subreddit');
}

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

public function posts() {
    return $this->belongsTo('App\Post');
}
public function subreddit() {
    return $this->belongsTo('App\Subreddit');
}

public function votes() {
    return $this->hasMany('App\Vote');
}

public function moderators() {
    return $this->hasMany('App\Moderator');
}

Vote模特

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

public function posts() {
    return $this->belongsTo('App\Post');
}

AuthServiceProvider中的网关授权

$gate->define('update-post', function ($user, $post, $isModerator) {
        // Check if user is subreddit owner
        if ($user->id === $post->subreddit->user->id) {
            return true;
        }

        // Check if user is the post author
        if ($user->id === $post->user_id) {
            return true;
        }

        // Check if user is a moderator of a subreddit
        if ($isModerator) {
            return true;
        }

        return false;
});

【问题讨论】:

  • 您的用户模型中是否有“版主”列?
  • 我在 User 模型中有一个 moderators() 方法和这个 return $this->hasManyThrough('App\Moderator', 'App\Subreddit');
  • 是的,但是Subreddit 模型中的moderators() 函数会在您的用户模型上查找moderators 键(您将“主持人”指定为第二个参数,即外键)
  • 我明白了。不,我的数据库中没有任何 moderators 字段。我应该删除它并使用 App\Moderator 而不是 App\User 吗?
  • @Drown 我已经更新了我的原帖,请看一下。

标签: php laravel model laravel-5 eloquent


【解决方案1】:

如果您想要所有版主的用户 ID 集合,请使用:

$ids = Post::findOrFail($id)->subreddit->moderators()->lists('user_id');

如果您想检查给定用户是否是 subreddit 的版主,请使用:

$subreddit = Post::findOrFail($id)->subreddit;

$isModerator = $subreddit->moderators()->where('user_id', $user->id)->exists();

【讨论】:

  • 非常感谢。我没有收到任何错误,这很好。但是我正在使用ids 来检查用户是否是属于 subreddit if ($user->id === $ids) 的版主,但这似乎不起作用,我无法在我是版主的 subreddits 中进行编辑。
  • @Halnex - 好吧,你显然没有指定 ID 的帖子。
  • Post::findOrFail($id)中的$id是从哪里来的?
  • 方法注入,还是路由模型绑定?如果您只是为Post 输入提示,您将得到一个空的帖子模型,其$idnull
  • 既然已经有帖子,为什么还要重新加载? $subreddit = $post->subreddit;
猜你喜欢
  • 2018-02-21
  • 2015-12-08
  • 2018-11-02
  • 2019-10-02
  • 2017-04-03
  • 2021-10-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多