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');
}
}
}
print_r($roles)and see what you getclass 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.$rolesto be an object, as shown by your debugging,$rolescontains1(not an object) which is why you're getting the error.public function users()(plural) topublic function user()(singular) in yourRoleclass