Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

lamka02sk's avatar

Base table or view not found: roles table [Very strange error]

I made user registration and it worked properly until I tried to register myself today and this strange error is showing.

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'ubytovaniesk.roles' doesn't exist (SQL: select * from `roles` where `name` = user limit 1)

Well, I know what this error means but I can't get rid of this because I didn't use roles table before and neither do I now. This happens when I am trying to create new user with method User::create while generating password with bcrypt. Any idea?

0 likes
7 replies
tykus's avatar

Do you have any Eloquent model event listeners/observers registered on the User model ?

lamka02sk's avatar

@tykus Negative. I am not that advanced Laravel user to use these features. It has to be definetely something with the password field, which is btw defined in fillable array, because when I remove password from create it stops on default value.

sujancse's avatar

Either you used protected $table = 'roles'; in your User model or used roles in form Validation Request.

tykus's avatar

Can you share the Controller code where you store the new User?

lamka02sk's avatar

@st8113 This is my registration form validator:

return Validator::make($data, [
            'username' => 'required|string|min:3|max:32|unique:users',
            'email' => 'required|string|email|max:64|unique:users',
            'password' => 'required|string|min:8|confirmed',
            'eula' => 'required|string'
        ]);```
I checked User model bude there is nothing to do with roles table...except if onetoone relationships would cause this.
lamka02sk's avatar

@tykus Of course:


namespace App\Http\Controllers\Auth;

use App\User;
use App\Http\Controllers\Controller;
use App\UsersSettings;
use App\VerifyAccount;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;

class RegisterController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Register Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users as well as their
    | validation and creation. By default this controller uses a trait to
    | provide this functionality without requiring any additional code.
    |
    */

    use RegistersUsers;

    private $user;
    private $verifyToken;
    
    public function __construct() {
        $this->middleware('guest');
    }
    
    public function showRegistrationForm() {
    
        return !Auth::check()
            ? view('admin.auth.register')
            : redirect('admin');
    
    }
    
    public function processRegister() {
    
        $data = Input::all();
        $validator = $this->validator($data);
        
        if(!$validator->passes() || !$this->create($data))
            return view('admin.auth.register', [
                'data' => $data,
                'errors' => array_unique($validator->errors()->all())
            ]);
        
        $this->createSettings();
        $this->createVerification();
        
        // TODO > Send mail with activation link
        
        return view('admin.auth.register', ['success' => true]);
    
    }
    
    /**
     * Get a validator for an incoming registration request.
     *
     * @param  array  $data
     * @return \Illuminate\Contracts\Validation\Validator
     */
    protected function validator(array $data) {
        
        return Validator::make($data, [
            'username' => 'required|string|min:3|max:32|unique:users',
            'email' => 'required|string|email|max:64|unique:users',
            'password' => 'required|string|min:8|confirmed',
            'eula' => 'required|string'
        ]);
        
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return \App\User
     */
    protected function create(array $data) {
        
        $this->user = User::create([
            'username' => $data['username'],
            'email' => $data['email'],
            'password' => bcrypt($data['password'], [
                'cost' => 12
            ]),
        ]);
        
        return $this->user;
        
    }
    
    private function createSettings() {
        
        UsersSettings::create([
            'user_id' => $this->user->id,
            'gender' => null,
            'firstname' => '',
            'lastname' => '',
            'street' => '',
            'city' => '',
            'zip' => '',
            'phone' => ''
        ]);
        
    }
    
    private function createVerification() {
    
        $this->verifyToken = VerifyAccount::create([
            'user_id' => $this->user->id,
            'code' => str_random(128),
            'active' => true
        ])->code;
    
    }
    
}
lamka02sk's avatar
lamka02sk
OP
Best Answer
Level 1

Ok, I finally figured it out. It was incomplete Voyager installation. I removed the whole Voyager package and it now works. Thank you everyone for your willingness!

Please or to participate in this conversation.