【问题标题】:Laravel get roles color and moreLaravel 获得角色颜色等
【发布时间】:2015-10-16 05:58:12
【问题描述】:

我正在尝试在自己的论坛软件中制作 RBAC。

到目前为止,权限有效,但问题是,当我想为用户名(MyBB 也有)添加颜色时,有些东西不起作用,而且我不正确理解它。

所以我有一个ForumController,里面有这段代码:

<?php

class ForumController extends \BaseController {

    public function index()
    {
        $forums = Forums::orderBy('disp_order', 'asc')->get();
        $categories = Categorie::orderBy('disp_order', 'asc')->get();

        return View::make('index')->with('forums', $forums)->with('categories', $categories);
    }

    public function forum($name)
    {
        $forums = Forums::where('name', '=', str_replace('Forum-', '',str_replace('-', ' ', $name)))->first();
        $categories = Categorie::orderBy('disp_order', 'asc')->get();
        return View::make('forum')->with('forums', $forums)->with('categories', $categories);
    }

    public function categorie($name)
    {
        $categories = Categorie::where('name', '=', str_replace('Categorie-', '',str_replace('-', ' ', $name)))->first();
        $threads = Thread::orderBy('date_posted', 'asc')->get();
        return View::make('categorie')->with('categories', $categories)->with('threads', $threads);
    }

    public function thread($title)
    {
        $thread = Thread::where('title', '=', str_replace('Thread-', '',str_replace('-', ' ', $title)))->first();
        $comments = Comment::orderBy('posted_at', 'asc')->get();
        return View::make('thread')->with('threads', $thread)->with('comments', $comments);
    }

}

很好,一切正常。

但现在我需要在函数 thread 中获取用户的角色。

我也有这些模型:

这些文件中只有 Eloquent 和 protected $table 的扩展。

我的role 表的方案如下所示:

我确实听说过有关 belongsTo 和 hasMany,但我真的不明白...

我希望能够为正确的用户提供正确的颜色。

所以用户表的方案:

我希望有人可以帮助我,因为我正在寻找答案很长时间。

我正在使用Laravel4

最诚挚的问候,

罗宾

【问题讨论】:

  • 评论不是与线程关联的吗?您的 Comment::orderBy('posted_at')-&gt;with('user.role')-&gt;get() 调用将返回所有线程上的所有 cmets,这似乎不是您想要的。假设 cmets 有一个 thread_id,您需要在 Thread 类中添加一个 public function comment() { $this-&gt;hasMany(Comment::class); },并使用 Thread::-&gt;with('comments.user.role') 急切加载线程

标签: php laravel-4 roles has-and-belongs-to-many rbac


【解决方案1】:

你是对的,你需要添加一些relationships

// In Comment.php, assuming that your comments table has a user_id field.
public function user()
{
    return $this->belongsTo(User::class);
}

// In User.php
public function role()
{
    return $this->belongsTo(Role::class);
}

然后将你的控制器调整为eager load这些关系。

$comments = Comment::orderBy('posted_at')->with('user.role')->get();

现在您可以在刀片模板中的注释旁边显示颜色,例如:

@foreach ($comments as $comment)
    <p>Color: {{ $comment->user->role->colour }}</p>
@endfoeach

【讨论】:

  • 当我尝试在刀片中访问它时,出现错误:Trying to get property of non-object
  • 我使用的代码与您给出的答案完全相同。
  • 抱歉,我的代码有点错误——现在已修复。如果 cmets 表有一个 user_id 字段,而 user 表有一个 role_id 字段,那么这两种关系都应该是“belongsTo”类型。请注意,您会得到 Trying to get property of non-object" if either the user_id or role_id are NULL`。
  • 回答后仍然遇到同样的问题。在我的comments 中,我有一个uid,而在我的user 表中只是id。角色以role_id 的形式存储在user 表中,并且在role 表中列出了角色(它们的功能和颜色等)。请注意,role 表中的 ID 设置为 rid
猜你喜欢
  • 1970-01-01
  • 2021-06-21
  • 2015-09-19
  • 2017-02-06
  • 2021-04-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-16
相关资源
最近更新 更多