Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/231.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php laravel多对多获取数据_Php_Laravel - Fatal编程技术网

Php laravel多对多获取数据

Php laravel多对多获取数据,php,laravel,Php,Laravel,我正在尝试根据用户角色使用多对多关系构建菜单。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`.`creat

我正在尝试根据用户角色使用多对多关系构建菜单。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');
    }

}
<?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 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());
}

乍一看,
用户角色
表中缺少时间戳列。 如果添加两列
处创建和
在处更新到表中,并将它们设置为日期时间它应该会为您解决问题


另外,从外观上看,您的
角色
表也没有上述时间戳。您应该添加这些变量,或者在
角色
模型中设置一个
公共静态变量
,以声明它们不存在。您可以通过编写
public static$timestamps=false

来实现这一点。乍一看,
user\u role
表中似乎缺少时间戳列。 如果添加两列
处创建和在处更新到表中,并将它们设置为日期时间它应该会为您解决问题


另外,从外观上看,您的
角色
表也没有上述时间戳。您应该添加这些变量,或者在
角色
模型中设置一个
公共静态变量
,以声明它们不存在。您可以通过编写
public static$timestamps=false

@papsurf来实现这一点,如果我不想/不能添加这些字段怎么办?有没有办法停用这些字段的提取?(尝试使用了
public static$hidden=array('created_at')
,但不起作用。@AeroCross是的,你可以!正如我上面解释的,你可以在模型的开头写
public static$timestamps=false;
。希望有帮助:)@papsurf如果我不想/不能添加这些字段怎么办?有没有办法停用这些字段的提取?(尝试使用了
public static$hidden=array('created_at')
,但不起作用。@AeroCross是的,你可以!正如我上面解释的那样,你可以在模型的开头写
public static$timestamps=false;
。希望有帮助:)
<?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());
}