dtvdz911959 2018-05-18 15:38
浏览 287
已采纳

laravel“SQLSTATE [HY000]:一般错误:1没有这样的表:user”但是'users'表存在

I am running Laravel 5.2, on Windows 8.1 using XAMPP with php 7.2, and I am trying to register a user using laravel auth register form with sqlite database. However when I try insert new record to table users I got error.

SQLSTATE[HY000]: General error: 1 no such table: user

When I migrate database it creates users table. But when I try to insert new record in users table with register form it tries to access user table. So I created user table in database it works fine but the record is inserted in users table and not in user table.

Migration

public function up(){
    Schema::create('users', function (Blueprint $table) {
        $table->increments('user_id');
        $table->string('name');
        $table->string('role');
        $table->string('username');
        $table->string('password');
        $table->rememberToken();
        $table->timestamps();
        });
    }

public function down(){
    Schema::drop('users');
}

User model

class User extends Authenticatable{
    protected $primaryKey = 'user_id';

    protected $fillable = [
        'name', 'role', 'username', 'password',
    ];

    protected $hidden = [
        'password', 'remember_token',
    ];
}

AuthController

namespace App\Http\Controllers\Auth;

use App\User;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;

class AuthController extends Controller{

    use AuthenticatesAndRegistersUsers, ThrottlesLogins;

    protected $redirectTo = '/';
    protected $username = 'username';

    public function __construct()
    {
        $this->middleware($this->guestMiddleware(), ['except' => 'logout']);
    }

    protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => 'required|max:255',
            'role' => 'required|max:7',
            'username' => 'required|unique:user',
            'password' => 'required|min:6|confirmed',
        ]);
    }

    protected function create(array $data)
    {
        return User::create([
            'name' => $data['name'],
            'role' => $data['role'],
            'username' =>$data['username'],
            'password' => bcrypt($data['password']),
        ]);
    }
}

thanks for help and sorry for bad english.

  • 写回答

2条回答 默认 最新

  • dongsigan2044 2018-05-18 15:44
    关注

    Your issue is in the validation:

    protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => 'required|max:255',
            'role' => 'required|max:7',
            'username' => 'required|unique:user',
            'password' => 'required|min:6|confirmed',
        ]);
    }
    

    You are checking if the username is unique in user not users, try this:

    protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => 'required|max:255',
            'role' => 'required|max:7',
            'username' => 'required|unique:users,username',
            'password' => 'required|min:6|confirmed',
        ]);
    }
    

    Also you will likely want to add a unique constraint on the migration.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题