Note that there are some explanatory texts on larger screens.

plurals
  1. POlaravel many to many fetching data
    primarykey
    data
    text
    <p>I am trying to build a menu according to user roles using many to many relationship. laravel is my first php framework and i am facing this issue</p> <pre><code>Unhandled Exception Message: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'user_role.created_at' in 'field list' SQL: SELECT `roles`.*, `user_role`.`id` AS `pivot_id`, `user_role`.`created_at` AS `pivot_created_at`, `user_role`.`updated_at` AS `pivot_updated_at`, `user_role`.`user_id` AS `pivot_user_id`, `user_role`.`role_id` AS `pivot_role_id` FROM `roles` INNER JOIN `user_role` ON `roles`.`id` = `user_role`.`role_id` WHERE `user_role`.`user_id` = ? Bindings: array ( 0 =&gt; 1, ) </code></pre> <p><strong>user migration:</strong></p> <pre><code>&lt;?php class Users { public function up() { Schema::create('users', function($table){ $table-&gt;engine = 'InnoDB'; $table-&gt;increments('id'); $table-&gt;string('username', 128); $table-&gt;string('password', 128); $table-&gt;string('firstname', 128); $table-&gt;string('lastname', 128); $table-&gt;date('dob'); $table-&gt;string('phone')-&gt;nullable(); $table-&gt;text('image')-&gt;nullable(); $table-&gt;timestamps(); }); DB::table('users')-&gt;insert(array( 'username' =&gt; 'admin', 'password' =&gt; Hash::make('admin'), 'firstname' =&gt; 'asdf', 'lastname' =&gt; 'zxcv', 'dob' =&gt; '1990-02-23', 'phone' =&gt; '935735367' )); } function down() { Schema::drop('users'); } } </code></pre> <p><strong>roles migration:</strong></p> <pre><code>&lt;?php class Role { public function up() { Schema::create('roles', function($table){ $table-&gt;engine = 'InnoDB'; $table-&gt;increments('id'); $table-&gt;string('lable', 60); $table-&gt;string('url', 128)-&gt;default("#"); $table-&gt;integer('parent')-&gt;default("0"); $table-&gt;integer('level')-&gt;default("0"); $table-&gt;integer('sort')-&gt;default("0"); $table-&gt;integer('published')-&gt;default("0"); }); } public function down() { Schema::drop('roles'); } } </code></pre> <p><strong>role_user</strong></p> <pre><code>&lt;?php class Access { public function up() { Schema::create('role_user', function($table){ $table-&gt;engine = 'InnoDB'; $table-&gt;increments('id'); $table-&gt;integer('user_id')-&gt;unsigned(); $table-&gt;integer('role_id')-&gt;unsigned(); $table-&gt;foreign('user_id')-&gt;references('id')-&gt;on('users'); $table-&gt;foreign('role_id')-&gt;references('id')-&gt;on('roles'); }); } public function down() { Schema::drop('role_user'); } } </code></pre> <p><strong>user model:</strong></p> <pre><code>&lt;?php class User extends Basemodel{ public static $table = 'users'; public static $timestamps = true; public static $rules = array( 'username' =&gt; 'required|min:3|alpha', 'password' =&gt; 'required|min:3|alpha' ); public function roles() { return $this-&gt;has_many_and_belongs_to('Role'); } public static function menu(){ $roles = User::find(1)-&gt;roles()-&gt;get(); return $roles; } } </code></pre> <p><strong>Role Model</strong></p> <pre><code>&lt;?php class Role extends Eloquent{ public static $table = 'roles'; } </code></pre> <p><strong>Controller:</strong></p> <pre><code>&lt;?php class Home_Controller extends Base_Controller { public $restful= true; public function get_index() { return View::make('home.index') -&gt;with('title','App Index') -&gt;with('menu',User::menu()); } </code></pre> <p>can someone guide me on what to do ? </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload