【发布时间】: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 的扩展。
我确实听说过有关 belongsTo 和 hasMany,但我真的不明白...
我希望能够为正确的用户提供正确的颜色。
所以用户表的方案:
我希望有人可以帮助我,因为我正在寻找答案很长时间。
我正在使用Laravel4
最诚挚的问候,
罗宾
【问题讨论】:
-
评论不是与线程关联的吗?您的
Comment::orderBy('posted_at')->with('user.role')->get()调用将返回所有线程上的所有 cmets,这似乎不是您想要的。假设 cmets 有一个thread_id,您需要在 Thread 类中添加一个public function comment() { $this->hasMany(Comment::class); },并使用Thread::->with('comments.user.role')急切加载线程
标签: php laravel-4 roles has-and-belongs-to-many rbac