我在laravel制作注册系统。当用户输入数据时,我将其存储在数据库中。但是当用户点击注册按钮时,它就会给我 跟随错误 未处理的例外
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
当我将表的名称更改为“user”时,然后说 未处理的例外
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
我该如何解决这个问题?
更新代码
以下是我的模特
user.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'
);
}
Basemodel.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();
}
}
}
答案 0 :(得分:2)
刚刚遇到这个问题,我可以准确地告诉你它是什么。仔细检查您的“unique:”验证约束。
您上面的唯一约束是“user”,因此laravel的Validator正在寻找该表来进行验证。
将其更改为“用户”,瞧!问题解决了。
答案 1 :(得分:0)
错误消息显示没有表“makeitsnappy”所以它似乎来自您在此处发布的代码IMO之外。