1

我正在 laravel 中制作注册系统。当用户输入数据时,我将其存储在数据库中。但是当用户单击注册按钮时,它会给我以下错误 Unhandled Exception

Message:

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'makeitsnappy.user' doesn't exist

SQL: SELECT COUNT(*) AS `aggregate` FROM `user` WHERE `username` = ?

Bindings: array (
  0 => 'asdasd',
)
Location:

/Applications/MAMP/htdocs/laravel-test/laravel/database/connection.php on line 264

当我将表的名称更改为“用户”时,会说未处理的异常

Message:

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'makeitsnappy.users' doesn't exist

SQL: INSERT INTO `users` (`username`, `password`, `updated_at`, `created_at`) VALUES (?, ?, ?, ?)

Bindings: array (
  0 => 'zafar',
  1 => '$2a$08$Xg47q9n65GZSS/l3xgYTyeTC7eICqMimf6KkvdFwSfiOgpFrI0tKe',
  2 => '2013-03-31 09:22:48',
  3 => '2013-03-31 09:22:48',
)
Location:

/Applications/MAMP/htdocs/laravel-test/laravel/database/connection.php on line 264

我怎么解决这个问题?

更新代码

这是我的模型

用户.php

class User extends Basemodel {
    public static $table = 'users';

    public static $rules = array(
        'username'=>'required|unique:user|alpha_dash|min:4',
        'password'=>'required|alpha_num|between:4,8|confirmed',
        'password_confirmation'=>'required|alpha_num|between:4,8'
    );
}

基本模型.php

class Basemodel extends Eloquent {
    public static function validate($data) {
        return Validator::make($data, static::$rules);
    }
}

这是我的 users.php 控制器

class Users_Controller extends Base_Controller {
    public $restful = true;

    public function get_new() {
        return View::make('users.new')->with('title', 'Make it Snappy - Register');
    }

    public function post_create() {
        $validation = User::validate(Input::all());

        if($validation->passes()) {
            User::create(array(
                    'username' => Input::get('username'),
                    'password' => Hash::make(Input::get('password'))
                ));

            return Redirect::to_route('home')->with('message', 'Thanks for registring');
        } else {
            return Redirect::to_route('register')->with_errors($validation)->with_input();
        }
    }
}
4

2 回答 2

2

刚刚经历了这个问题,我可以确切地告诉你它是什么。仔细检查您的“唯一:”验证约束。

您上面的唯一约束是“用户”,所以 laravel 的验证器正在寻找该表来进行验证。

将其更改为“用户”,瞧!问题解决了。

于 2013-11-12T01:45:52.130 回答
0

错误消息说没有表“makeitsnappy”,所以这似乎来自您在 IMO 此处发布的代码之外。

于 2013-04-05T12:44:26.303 回答