rotschaedl
rotschaedl

Reputation: 7

Laravel Doesn't Use the Right Table

Laravel picks the wrong table, the users table. I have switched the table in config/auth to the correct one but for some reason, Laravel still uses the default users table.

Code

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Auth\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;

class UserLoginController extends Controller
{
    protected $table = 'pupil';

    public function showLoginForm()
    {
        return view('home');
    }

    public function login(Request $request)
    {
        $this->validate($request, [
            'user' => 'required',
            'password' => 'required|min:3'
        ]);

        $user = User::where('accountName', $request->user)
            ->where('password', $request->password)
            ->first();

        if ($user) {
            Auth::login($user);
            return redirect()->route('neue_anmeldung');
        }

        return redirect()->back()->withInput('email');
    }
}

I get the following error message.

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'database1.users' doesn't exist (SQL: select * from users where accountName = 6001 and password = password limit 1)

Upvotes: 0

Views: 540

Answers (2)

ManojKiran
ManojKiran

Reputation: 6341

By default eloquent Will assumes the tables as follows

For Example

If Your Model name is

User Then it will search for users table in database

AdminUser Then it will search for admin_users table in database

To Change the default tablename assumed by eloquent

Open Your Current Model

Not Your Controller and protected $table = 'yourTableNAme';

Hope it helps

Upvotes: 0

user3647971
user3647971

Reputation: 1056

Your User model is configured to use the users table. You have defined the table in your controller which is wrong. You need to define the property protected $table = 'pupil'; in your App\User.php

Upvotes: 1

Related Questions