将版主添加到论坛类别时,没有模型[App \ Subreddit]的查询结果

时间:2021-06-20 07:59:40

I am trying to assign users as moderators to a forum category. At the moment, I am only trying to display route where a user can add moderators subreddit/{id}/moderators and display the subreddit name.

我正在尝试将用户指定为论坛类别的主持人。目前,我只是试图显示用户可以添加版主subreddit / {id} /版主并显示subreddit名称的路线。

For that, I am getting No query results for model [App\Subreddit]

为此,我得到的模型没有查询结果[App \ Subreddit]

I must've messed up the relations between the tables somewhere.

我一定搞砸了桌子之间的关系。

My tables

我的桌子

#users: id, name, email...
#subreddits: id, name, user_id...
#moderators: id, user_id, subreddit_id

Routes.php

routes.php文件

Route::get('subreddit/{id}/moderators', [
    'as' => 'moderators',
    'uses' => 'ModeratorsController@create'
]);

Moderator.php Model

Moderator.php模型

class Moderator extends Model
{
    protected $table = 'moderators';

    protected $fillable = ['user_id', 'subreddit_id'];

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

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

In User Model, I have a hasManyThrough

在用户模型中,我有一个hasManyThrough

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

In Subreddit Model

在Subreddit模型中

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

And then in ModeratorsController I have the following create() method

然后在ModeratorsController中我有以下create()方法

public function create(Moderator $moderator, Subreddit $subreddit, User $user)
{
    $subreddit = Subreddit::with('user')->findOrFail($subreddit->id);

    return view('user/moderators')->with('subreddit', $subreddit)->with('user', $user)->with('moderator', $moderator);
}

If I change findOrFail to firstOrFail it will get me the first subreddit in the database, but I don't want the first, I want the exact one I'm trying to add moderators to.

如果我将findOrFail更改为firstOrFail,它将获得数据库中的第一个subreddit,但我不想要第一个,我想要我想要添加版主的确切的一个。

1 个解决方案

#1


1  

class User extends Model{
    public function canModerate(){ //name this as you wish
        return $this->belongsToMany('App\Subreddit','moderators');
    }
    public function subreddits(){
        return $this->hasMany('App\Subreddit');
    }
}

class Subreddit extends Model{
    public function moderators(){ // name this as you wish
        return $this->belongsToMany('App\User','moderators');
    }

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

remove id from moderators table and you wont need the Moderator class. Additional documentation can be found here.

从版主表中删除id,你不需要Moderator类。可以在此处找到其他文档。

Edit RouteServiceProvider::boot method and add the following line:

编辑RouteServiceProvider :: boot方法并添加以下行:

$router->model('subreddit', 'App\Subreddit');

Documentation can be found here.

文档可以在这里找到。

Your route should then look like:

您的路线应如下所示:

Route::resource('subreddit.moderator','ModeratorsController');

The URL should look like http://localhost/subreddit/{subreddit}/moderator/create

URL应该类似于http:// localhost / subreddit / {subreddit} / moderator / create

And finally the controller method should be:

最后控制器方法应该是:

public function create(Subreddit $subreddit)
{
    $user = $subreddit->creator;
    $moderators = $subreddit->moderators()->get();
    return view('user/moderators')->with(compact('subreddit','user','moderators'));
}

#1


1  

class User extends Model{
    public function canModerate(){ //name this as you wish
        return $this->belongsToMany('App\Subreddit','moderators');
    }
    public function subreddits(){
        return $this->hasMany('App\Subreddit');
    }
}

class Subreddit extends Model{
    public function moderators(){ // name this as you wish
        return $this->belongsToMany('App\User','moderators');
    }

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

remove id from moderators table and you wont need the Moderator class. Additional documentation can be found here.

从版主表中删除id,你不需要Moderator类。可以在此处找到其他文档。

Edit RouteServiceProvider::boot method and add the following line:

编辑RouteServiceProvider :: boot方法并添加以下行:

$router->model('subreddit', 'App\Subreddit');

Documentation can be found here.

文档可以在这里找到。

Your route should then look like:

您的路线应如下所示:

Route::resource('subreddit.moderator','ModeratorsController');

The URL should look like http://localhost/subreddit/{subreddit}/moderator/create

URL应该类似于http:// localhost / subreddit / {subreddit} / moderator / create

And finally the controller method should be:

最后控制器方法应该是:

public function create(Subreddit $subreddit)
{
    $user = $subreddit->creator;
    $moderators = $subreddit->moderators()->get();
    return view('user/moderators')->with(compact('subreddit','user','moderators'));
}