Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
307 views
in Technique[技术] by (71.8m points)

How to solve this problem in laravel 8??? 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 IlluminateDatabaseMigrationsMigration;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateSupportFacadesSchema;

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 AppHttpControllersAuth;

use AppHttpControllersController;
use AppModelsUser;
use AppProvidersRouteServiceProvider;
use IlluminateAuthEventsRegistered;
use IlluminateHttpRequest;
use IlluminateSupportFacadesAuth;
use IlluminateSupportFacadesHash;

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

    /**
     * Handle an incoming registration request.
     *
     * @param  IlluminateHttpRequest  $request
     * @return IlluminateHttpRedirectResponse
     *
     * @throws IlluminateValidationValidationException
     */
    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 AppModels;

use IlluminateContractsAuthMustVerifyEmail;
use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateFoundationAuthUser as Authenticatable;
use IlluminateNotificationsNotifiable;

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])

Please help me. Best Regards.

question from:https://stackoverflow.com/questions/66063589/how-to-solve-this-problem-in-laravel-8-sqlstate42s02-base-table-or-view-no

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Since you are using a different table name for user you have to defined it in your model.by default laravel convention is when your model doesn't have table property then it will look for the table with plural name of model(users).

Add this to user model:

protected $table='tbl_user';


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...