多対多の関係を利用して、ユーザーの役割に応じたメニューを作成しようとしています。laravelは私の最初のphpフレームワークであり、私はこの問題に直面しています
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 => 1,
)
ユーザーの移行:
<?php
class Users {
public function up()
{
Schema::create('users', function($table){
$table->engine = 'InnoDB';
$table->increments('id');
$table->string('username', 128);
$table->string('password', 128);
$table->string('firstname', 128);
$table->string('lastname', 128);
$table->date('dob');
$table->string('phone')->nullable();
$table->text('image')->nullable();
$table->timestamps();
});
DB::table('users')->insert(array(
'username' => 'admin',
'password' => Hash::make('admin'),
'firstname' => 'asdf',
'lastname' => 'zxcv',
'dob' => '1990-02-23',
'phone' => '935735367'
));
}
function down()
{
Schema::drop('users');
}
}
役割の移行:
<?php
class Role {
public function up()
{
Schema::create('roles', function($table){
$table->engine = 'InnoDB';
$table->increments('id');
$table->string('lable', 60);
$table->string('url', 128)->default("#");
$table->integer('parent')->default("0");
$table->integer('level')->default("0");
$table->integer('sort')->default("0");
$table->integer('published')->default("0");
});
}
public function down()
{
Schema::drop('roles');
}
}
role_user
<?php
class Access {
public function up()
{
Schema::create('role_user', function($table){
$table->engine = 'InnoDB';
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->integer('role_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
$table->foreign('role_id')->references('id')->on('roles');
});
}
public function down()
{
Schema::drop('role_user');
}
}
ユーザーモデル:
<?php
class User extends Basemodel{
public static $table = 'users';
public static $timestamps = true;
public static $rules = array(
'username' => 'required|min:3|alpha',
'password' => 'required|min:3|alpha'
);
public function roles()
{
return $this->has_many_and_belongs_to('Role');
}
public static function menu(){
$roles = User::find(1)->roles()->get();
return $roles;
}
}
ロールモデル
<?php
class Role extends Eloquent{
public static $table = 'roles';
}
コントローラ:
<?php
class Home_Controller extends Base_Controller {
public $restful= true;
public function get_index()
{
return View::make('home.index')
->with('title','App Index')
->with('menu',User::menu());
}
誰かが私に何をすべきかを教えてもらえますか?