Laravel加入了每个人的可见性

时间:2015-07-09 19:18:21

标签: php laravel join laravel-4 roles

我想在Laravel 4中建立一个论坛(不要问我为什么哈哈)。

但是,我想在其中获得一个RBAC系统,因此我可以显示用户颜色等...

问题是,它不能很好地运作......

我得到了什么:

ForumController

public function thread($id)
{
    $thread = ForumThread::with('role')->find($id);
    if ($thread == null)
    {
        return Redirect::route('home')->with('fail', "That thread doesn't exist.");
    }
    $author = $thread->author()->first()->username;

    $role_id = $thread->author()->first()->role_id;

    $role_id2 = $thread->comments()->first()->author_id;

    $color = Role::where('id', '=', $role_id)->first()->role_colour;

    $color2 = User::where('user.id', '=', $role_id2)->join('roles', 'roles.id', '=', 'user.role_id')->join('comments', 'comments.author_id', '=', 'user.id')->first()->role_colour;

    return View::make('forum.thread')->with('thread', $thread)->with('author', $author)->with('color', $color)->with('color2', $color2);
}

所以我想让$color2工作。

现在他只显示第一篇文章的第一个值。

因此,ID 4可能是第一个发表评论的人,它将使用id为4的用户的role_id。

但是我希望得到每个评论海报的颜色,以便适当地展示。

Database schemes:

user: user scheme roles Roles scheme comments Comments scheme

希望有人可以帮助我:)。

修改

我的模特:

<?php
class ForumThread extends Eloquent
{
    protected $table = 'threads';

    public function group()
    {
        return $this->belongsTo('ForumGroup');
    }
    public function category()
    {
        return $this->belongsTo('ForumCategory', 'category_id');
    }
    public function comments()
    {
        return $this->hasMany('ForumComment', 'thread_id');
    }
    public function author()
    {
        return $this->belongsTo('User', 'author_id');
    }
    public function role()
    {
        return $this->hasMany('Role', 'id');
    }
}


<?php
class ForumComment extends Eloquent
{
    protected $table = 'comments';

    public function group()
    {
        return $this->belongsTo('ForumGroup');
    }
    public function category()
    {
        return $this->belongsTo('ForumCategory');
    }
    public function thread()
    {
        return $this->belongsTo('ForumThread');
    }
    public function author()
    {
        return $this->belongsTo('User');
    }
    public function role()
    {
        return $this->hasMany('Role', 'id');
    }
}

1 个答案:

答案 0 :(得分:0)

好的,根据您的最新评论和您的模型更新,您应该尝试类似

的内容
$author = $thread->author();
$comment = Comment::where('author_id', $author->id)->first(); 
$role = $author->role();
$color2 = $role->role_colour;