[英]SQLSTATE[42S02]: Base table or view not found: 1146
I'm trying to login as Administrator. 我正在尝试以管理员身份登录。 I used Multiple authentication one for my User that works fine and second for Admin that I'm trying to login but when I insert right values in my database which I manually inserted the values using Seeding it gives me a error which I don't know the cause. 我对我的用户使用了多重身份验证,一种可以很好地工作;对于我尝试登录的Admin ,它可以进行第二种身份验证,但是当我在数据库中插入正确的值时,我使用“ 种子”手动插入了值,这给了我一个我不知道的错误原因。
What I did is I configure my auth.php So I can have two authentications for User and Admin. 我所做的是配置了auth.php,因此我可以对User和Admin进行两种身份验证。
<?php
return [
'defaults' => [
'guard' => 'web',
'passwords' => 'users',
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
],
'admins' => [
'driver' => 'session',
'provider' => 'admins'
]
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Models\User::class,
],
// For admin
'admins' => [
'driver' => 'eloquent',
'model' => App\Models\Admin::class
]
// 'users' => [
// 'driver' => 'database',
// 'table' => 'users',
// ],
],
'passwords' => [
'users' => [
'provider' => 'users',
'email' => 'auth.emails.password',
'table' => 'password_resets',
'expire' => 60,
],
'admins' => [
'provider' => 'admins',
'email' => 'auth.emails.password',
'table' => 'password_resets',
'expire' => 60,
],
],
];
create_admins_table - Migrations create_admins_table-迁移
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateAdminsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('admins', function (Blueprint $table)
{
$table->increments('id');
$table->string('username');
$table->string('password');
$table->timestamps();
});
}
}
My migration files are set. 我的迁移文件已设置。 So I migrated it. 所以我迁移了它。
Admin - Models 管理员 -模型
<?php
namespace App\Models;
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
class Admin extends Model implements AuthenticatableContract
{
use Authenticatable;
protected $guard = "admins";
protected $table = 'admins';
protected $fillable = [
'username',
'password',
];
protected $hidden = [
'password',
'remember_token'
];
}
AdminTableSeeder - Seeds AdminTableSeeder-种子
<?php
use Illuminate\Database\Seeder;
class AdminTableSeeder extends Seeder
{
public function run()
{
DB::table('admins')->delete();
$admins = array(
array('username' => 'admin',
'password' => Hash::make('admin'))
);
DB::table('admins')->insert($admins);
}
}
AdminController - Controllers AdminController-控制器
<?php
namespace App\Http\Controllers;
use Auth;
use App\Models\Admin;
use Illuminate\Http\Request;
class AdminController extends Controller
{
public function postAdminLogin(Request $request)
{
$this->validate($request,
[
'username' => 'required',
'password' => 'required|min:7',
]);
if(!Auth::attempt($request->only(['username','password']), $request->has('remember')))
{
dd('error');
}
dd('Succesful!');
}
}
UPDATE!! 更新!
So I changed my username and password to letmein
to avoid my confusion. 因此,为了避免混乱,我将用户名和密码更改为letmein
。 I attempt to login the right values but I can't still login. 我尝试登录正确的值,但仍然无法登录。 !Auth:attempt
won't match my values in my database and prints me a "error" message. !Auth:attempt
与数据库中的值不匹配,并向我显示“错误”消息。
public function postAdminLogin(Request $request)
{
$this->validate($request,
[
'username' => 'required',
'password' => 'required|min:7',
]);
if(!Auth::attempt($request->only(['username','password']), $request->has('remember')))
{
dd('error');
}
dd('Succesful!');
}
admin.blade.php admin.blade.php
<form class = "form-vertical" role = "form" method = "post" action = "{{ route ('auth.admin') }}">
<div class = "form-group {{ $errors->has('username') ? 'has-errors' : '' }}">
<label for = "username" class = "control-label">Username</label>
<input type = "text" name = "username" class = "form-control">
@if ($errors->has('username'))
<span class = "help-block">{{ $errors->first('username') }}</span>
@endif
</div>
<div class = "form-group {{ $errors->has('password') ? 'has-errors' : '' }}">
<label for = "password" class = "control-label">Password</label>
<input type = "password" name = "password" class = "form-control">
@if ($errors->has('password'))
<span class = "help-block">{{ $errors->first('password') }}</span>
@endif
</div>
<div class = "checkbox">
<label>
<input type = "checkbox" name = "remember">Remember me</input>
</label>
</div>
<div class = "form-group">
<button type = "submit" class = "btn btn-default">Login</button>
</div>
<input type = "hidden" name = "_token" value = "{{ Session::token() }}">
</form>
Your Table name is in Question here... We think it should have been "admins" in the AdminTableSeeder like so: 您的表名在这里有问题...我们认为它应该是AdminTableSeeder中的“管理员”,如下所示:
use Illuminate\Database\Seeder;
class AdminTableSeeder extends Seeder {
public function run() {
DB::table('admins')->delete(); //<== "admins" AS DEFINED IN YOUR CONFIG::: NOT "admin"
$admin = array(
array('username' => 'admin',
'password' => Hash::make('admin'))
);
DB::table('admins')->insert($admin);
}
}
AdminTableSeeder - Seeds AdminTableSeeder-种子
Use truncate instead of delete 使用截断而不是删除
use Illuminate\Database\Seeder;
class AdminTableSeeder extends Seeder
{
public function run()
{
DB::table('admins')->truncate();
$admins = array(
array('username' => 'admin',
'password' => Hash::make('admin'))
);
DB::table('admins')->insert($admins);
}
}
The SQL query next to the error message says 错误消息旁边的SQL查询说
select count(*) as aggregate from `admin` where `password`=admin
Now, in your WHERE clause password is backticked, so I assume it is a column(not a table). 现在,在您的WHERE子句中,密码被反引号,因此我假设它是一列(不是表)。 Backticks are usually used in SQL for table names or column names. 反引号通常在SQL中用于表名或列名。 But the you compare the column name to admin, without ticks. 但是,您将列名与admin进行了比较,没有打勾。 'admin' is not a reserved word, and it is not quoted in your query as a text string should be, so the database server then starts looking for a table with that name to match. “ admin”不是保留字,并且在查询中未使用引号将其引为文字字符串,因此数据库服务器随后开始查找具有该名称的表以进行匹配。
So, have you tried adding quotes there? 那么,您是否尝试过在其中添加引号?
SELECT COUNT(*) AS aggregate FROM `admin` WHERE `password`='admin'
Edit: 编辑:
If all else fails you might also want to simply dump the database queries being issued. 如果所有其他方法都失败了,您可能还想简单地转储正在发出的数据库查询。 Add a listener/dump: 添加侦听器/转储:
\DB::listen(function($sql, $bindings, $time) {
var_dump($sql);
var_dump($bindings);
var_dump($time);
});
..and check the output. ..并检查输出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.