Laravel 不使用权利 Table

Laravel Doesn't Use the Right Table

Laravel选错了table,userstable。我已将 config/auth 中的 table 切换为正确的,但出于某种原因,Laravel 仍使用默认值 users table.

代码

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');
    }
}

我收到以下错误消息。

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)

您的 User 模型配置为使用 users table。您在 controller 中定义了 table,这是错误的。您需要在 App\User.php

中定义 属性 protected $table = 'pupil';

默认情况下 eloquent 将假定 table 如下

For Example

如果您的模特名称是

User 然后它将在数据库

中搜索users table

AdminUser 然后它将在数据库

中搜索admin_users table

更改 eloquent

假定的默认值 tablename

打开您当前的模型

Not Your Controllerprotected $table = 'yourTableNAme';

希望对您有所帮助