i have 2 table and a pivot table. structure of the table is mentioned below.
users table:
id username password
1 admin xyzasd
roles table:
id name
1 role1
2 role2
role_user table:
id user_id role_id
1 1 1
2 1 2
My user model:
class User extends Basemodel{
public static $table = 'users';
public static $timestamps = true;
public function roles()
{
return $this->has_many_and_belongs_to('Role');
}
public static function menu(){
$roles = User::find(1)->roles()->get();
return $roles;
}
}
my controller:
public function get_index()
{
return View::make('home.index')
->with('title','testing many to many')
->with('menu',User::menu());
}
and in my blade view file i have this statement {{$menu}} all i get is a message array, can some one guide me on how to fetch the records ?
First, User should extend Eloquent. Also, you should create a Role model.
//application/models/User.php
//application/models/Role.php
Then for your controller you basically can just pass the user.
Then in your view you can do cool stuff like: