0
votes

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)

2
Still doesn't work. - rotschaedl
You uses User Model thats why it's searching a users table mention your Model name carefully - MD. Jubair Mizan
so i have to rename My Users.php in app/ to the table i want to use? - rotschaedl

2 Answers

1
votes

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

0
votes

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