【问题标题】:Laravel Doesn't Use the Right TableLaravel 没有使用正确的表
【发布时间】:2019-06-20 02:01:24
【问题描述】:

Laravel 选择了错误的表,users 表。我已将config/auth 中的表格切换到正确的表格,但由于某种原因,Laravel 仍然使用默认的users 表格。

代码

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]:未找到基表或视图:1146 表 'database1.users' 不存在(SQL: select * from users where accountName = 6001 和 password = 密码限制 1)

【问题讨论】:

  • 还是不行。
  • 您使用用户模型,这就是为什么它在搜索用户表时要仔细提及您的模型名称
  • 所以我必须将 app/ 中的 My Users.php 重命名为我要使用的表?

标签: php laravel laravel-artisan


【解决方案1】:

您的User 模型配置为使用users 表。您在controller 中定义了表格,这是错误的。您需要在 App\User.php 中定义属性 protected $table = 'pupil';

【讨论】:

    【解决方案2】:

    默认情况下eloquent将假设表格如下

    For Example

    如果您的模型名称是

    User 然后它会在数据库中搜索users

    AdminUser 然后它会在数据库中搜索admin_users

    更改eloquent 假定的默认tablename

    打开您当前的模型

    Not Your Controllerprotected $table = 'yourTableNAme';

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 2017-01-30
      • 1970-01-01
      • 2023-03-18
      • 2018-04-14
      • 1970-01-01
      • 2015-12-21
      • 2014-07-18
      • 2019-03-25
      • 1970-01-01
      相关资源
      最近更新 更多