0

I'm trying to make a RBAC in my own forum software.

For so far, the permissions work, but the problem is, when I want to add colors to usernames (what MyBB also has) something doesn't work and I don't understand it propperly.

So I have an ForumController with this code inside:

<?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);
    }

}

Good, everything of that works.

But now I need to get the roles for users inside of the function thread.

I also have these models:

models

There is only an extend to Eloquent and the protected $table inside of these files.

The scheme of my role table looks like this: scheme

I did hear somethig about belongsTo and hasMany, but I really don't understand it...

I want to be able to get the right color on the right user.

So the scheme of the user table:

user scheme

I hope someone can help me out, because I'm looking for the answer a long time.

I'm using Laravel4

Kindest regards,

Robin

Robin
  • 1,567
  • 3
  • 25
  • 67
  • Aren't Comments associated with Threads? Your `Comment::orderBy('posted_at')->with('user.role')->get()` call will return all comments on all threads, which doesn't seem like what you want. Assuming comments have a `thread_id`, you'd want to add a `public function comment() { $this->hasMany(Comment::class); }` to your Thread class, and eager load threads using `Thread::->with('comments.user.role')` – Ben Claar Jul 25 '15 at 20:19

1 Answers1

0

You're right, you need to add some 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);
}

Then adjust your controller to eager load these relationships.

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

Now you can show the color next to a comment in your blade template like:

@foreach ($comments as $comment)
    <p>Color: {{ $comment->user->role->colour }}</p>
@endfoeach
Ben Claar
  • 3,285
  • 18
  • 33
  • When I try to access it in my blade, I get an error: `Trying to get property of non-object` – Robin Jul 26 '15 at 19:03
  • I used the exact same code as in your answer given. – Robin Jul 26 '15 at 20:40
  • Sorry, I got the code a bit wrong -- fixed now. If the comments table has a user_id field and the user table has a role_id field, both relationships should be the "belongsTo" type. Note that you'll get that `Trying to get property of non-object" if either the user_id or role_id are `NULL`. – Ben Claar Jul 27 '15 at 01:03
  • Still getting the same issue after your answer. In my `comments`, I got a `uid` and in my `user` table is it just `id`. The roles are stored in the `user` table as `role_id` and the role are listed up (their funcion and color etc...) in he `role` table. Be aware of that the ID in the `role` table is set as `rid`. – Robin Jul 27 '15 at 07:12