'error in Laravel8: SQLSTATE[42S02]: Base table or view not found: 1146 Table

I have started a new project with Laravel 8. I use the starter kit Laravel Breeze. But I can't customize fields. I have changed fields in the migration and Register Controller and User model.

here is my code:


migration file.

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class TblUsers extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('tbl_users', function (Blueprint $table) {
            $table->id();
            $table->string('fullname');
            $table->string('username');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('phone');
            $table->string('organization_type');
            $table->string('community_dev_auth_id');
            $table->string('file_names');
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('tbl_users');
    }
}

register controller file.

<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Models\User;
use App\Providers\RouteServiceProvider;
use Illuminate\Auth\Events\Registered;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;

class RegisteredUserController extends Controller
{
    /**
     * Display the registration view.
     *
     * @return \Illuminate\View\View
     */
    public function create()
    {
        return view('auth.register');
    }

    /**
     * Handle an incoming registration request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\RedirectResponse
     *
     * @throws \Illuminate\Validation\ValidationException
     */
    public function store(Request $request)
    {
        $request->validate([
            'fullname' => 'required|string|max:255',
            'username' => 'required|string|max:255',
            'email' => 'required|senter code heretring|email|max:255|unique:users',
            'phone' => 'required|string|max:255',
            'organization' => 'required|string|max:255',
            'community' => 'required|string|max:255',
            // 'phone' => 'required|string|max:255',
            'password' => 'required|string|min:8',
        ]);

        Auth::login($user = User::create([
            'fullname' => $request->fullname,
            'username' => $request->username,
            'email' => $request->email,
            'phone' => $request->phone,
            'organization_type' => $request->organization,
            'community_dev_auth_id' => $request->community,
            'password' => Hash::make($request->password),
        ]));
        

        event(new Registered($user));

        return redirect(RouteServiceProvider::HOME);
    }
}

user model file.

<?php

namespace App\Models;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable
{
    use HasFactory, Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'fullname',
        'username',
        'email',
        'phone',
        'organization',
        'community',
        'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];
}

I have run this project, but it returns this error:

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'ambulance_dubai.users' doesn't exist (SQL: select count(*) as aggregate from users where email = [email protected])



Solution 1:[1]

Since you are using a different table name for the user model you have to define it in your model. By default, Laravel will look for the plural name of a model(users) if your model doesn't have a table property.

Add this to the user model:

protected $table='tbl_user';

Solution 2:[2]

first, you will check user table was migrate, the user table not to be migrated use this command

php artisan migrate

Open your User Model

and add $table

class User extends Authenticatable {
  protected $table = 'users';
}

Solution 3:[3]

Another cause could be that the validation has a different name for the table. For example, having the table tbl_users in the validation could exist an error and have: 'required|unique:tbl_user,email'.

The letter "s" is missing and the error would be thrown.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 James Martinez
Solution 2 Menisha Myelwaganam
Solution 3 jesusd0897