【问题标题】:laravel many to many fetching datalaravel 多对多获取数据
【发布时间】:2013-01-09 18:57:35
【问题描述】:

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

有人可以指导我做什么吗?

【问题讨论】:

    标签: php laravel


    【解决方案1】:

    乍一看,user_role 表中缺少时间戳列。 如果添加两列; created_atupdated_at 到表中并将它们设置为 datetime 它应该会为你解决它!

    从外观上看,您的roles 表也没有上述这些时间戳。您应该添加这些或在Role 模型中设置public static variable 以声明它们不存在。你可以写public static $timestamps = false

    【讨论】:

    • @PapaSmurf 如果我不想/不能添加这些字段怎么办?有什么方法可以停用这些字段的获取? (尝试使用public static $hidden = array('created_at'),但没有成功。
    • @AeroCross 是的,你可以!正如我在上面解释的那样,您可以在模型的开头写上public static $timestamps = false;。希望有帮助:)
    猜你喜欢
    • 2014-10-14
    • 2015-04-05
    • 2019-05-23
    • 2019-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多