0

I followed some tutorial on http://laravel.com/docs/ and it just won't work. Here's what I tried:

Role model:

<?php

class Role extends Eloquent {

    protected $table = 'accounts';

    public function users()
    {
        return $this->belongsToMany('User');
    }
}

RoleController.php

<?php

class RoleController extends BaseController {

    public function get_role()
    {
        $roles = User::find(16)->group_id;

        if ($roles->contains(3))
        {
            echo 'this works';
        }
        else
        {
            return Redirect::to('news/index');
        }
    }
}

And my route:

Route::get('dash', 'RoleController@get_role');

And here's my error:

Call to a member function contains() on a non-object

It says the error is on line:

if ($roles->contains(3))

Like the contains method is... I don't know hah.

Also, $roles = User::find(16)->group_id; The 16 is the id of the account, right? And the group_id is the table?

Thanks in advance.

EDIT:

Here's the solution, I've changed it in RoleController.php:

<?php

class RoleController extends BaseController {

    public function get_role()
    {
        $roles = User::find(16)->group_id;

        if (if ($roles == '1')
        {
            echo 'this works';
        }
        else
        {
            return Redirect::to('news/index');
        }
    }
}
8
  • do a print_r($roles) and see what you get Commented Aug 2, 2013 at 13:30
  • I did it like this: class RoleController extends BaseController { public function get_role() { $roles = User::find(16)->group_id; print_r($roles); } } and it just displays 1 :p Also I tried with the contain method to set it to 1, but it still does that error. Commented Aug 2, 2013 at 13:33
  • Your script expects $roles to be an object, as shown by your debugging, $roles contains 1 (not an object) which is why you're getting the error. Commented Aug 2, 2013 at 13:35
  • Aha, got it. But how could I make it a non-object, or whatever does it take to make it works? Should change the method or something? Commented Aug 2, 2013 at 13:38
  • Try changing public function users() (plural) to public function user() (singular) in your Role class Commented Aug 2, 2013 at 13:42

2 Answers 2

1

This line:

$roles = User::find(16)->group_id;

means you are asking "find user 16, and give me the group_id of that user". This is the "1" that you refer to (in your comments).

Since you have the group_id - change

if ($roles->contains(3))

to

if ($roles == '3')
Sign up to request clarification or add additional context in comments.

Comments

0

here's the solution, I've changed it in RoleController.php:

    <?php

class RoleController extends BaseController {

    public function get_role() {
$roles = User::find(16)->group_id;

            if (if ($roles == '1')
             {
               echo 'this works';
             } else {

               return Redirect::to('news/index');
                    }
             }
}

1 Comment

Could you update your answer to include where you change that code? A bigger snippet (I know you can infer it by reading the question, but it's better if the answer is self-sufficient)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.