Inu Bayu Aji
Inu Bayu Aji

Reputation: 108

laravel "SQLSTATE[HY000]: General error: 1 no such table: user" but 'users' table exists

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.

Upvotes: 1

Views: 12792

Answers (5)

Takkam Darios
Takkam Darios

Reputation: 71

This could be because you tried using a different name in your model fillable .Try to make sure that the names are identical for example you can not use user_id in your migration database table then you use users_id in your user model

Upvotes: 0

Mudimba Moonde
Mudimba Moonde

Reputation: 35

In your users model add public $table = 'users' | public $table = 'user'

Upvotes: 1

PaulH
PaulH

Reputation: 3049

I had a similar error. It was due to

Schema::create('tasks', function (Blueprint $table) {
    $table->foreignId('owner_id')->nullable()->constrained('user')->index();

instead of (note the s in users)

    $table->foreignId('owner_id')->nullable()->constrained('users')->index();

Upvotes: 0

Fireynis
Fireynis

Reputation: 437

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.

Upvotes: 5

Saurabh
Saurabh

Reputation: 2775

Please refer to this: https://laravel.com/docs/5.6/eloquent#eloquent-model-conventions

If you have a model User, then you must have the table as "users" not "user" in your database.

Upvotes: 0

Related Questions