laravel 如何解决“SQLSTATE[42S22]:列未找到:1054未知列'0'在'where子句'”

d7v8vwbk  于 2023-11-20  发布在  其他
关注(0)|答案(1)|浏览(148)

下面是我得到的错误截图。


的数据

登录中使用的LoginController代码如下所示

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Hash;
use App\Models\Customer;

class LoginController extends Controller
{
    public function login(){
        return view('user.login');
    }
    public function check(Request $request)
    {
        $credentials= $request->validate([
            'username' => ['required'],
            'password' => ['required'],
        ]);
        if(Auth::attempt([$credentials])){
            return view('welcome');
        }
        else{
            return view('unauth');
        }
    }
}

字符串

Customer.php模型如下所示

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Customer extends Model
{
    use HasFactory;

    protected $fillable=[
        'name',
        'username',
        'password',
        'email',
        'invoice',
        'kwh',
        'warranty',
        'date'
    ];
}

web.php中的代码如下所示

Route::get('/login', [LoginController::class,'login']);
Route::post('/check',[LoginController::class,'check'])->name('check');
Route::get('/unauthorizedaccess', function () {
    return view('unauth');
});

Login.blade.php代码如下

<!doctype html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
    <link href="{{asset('css/customs.css')}}" rel="stylesheet" >
    <title>Login Form</title>
  </head>
  <body style=" background-image: url({{url('img/img-1.webp')}});">
        <div class="container-fluid">
            <form action="{{ route('check') }}" method="POST" class="mx-auto formlogin">
              {!! csrf_field() !!}
                <div style="text-align: center;">
                    <img src="{{url('img/logo.webp')}}" alt="logo" width="170px;">
                </div>
                <div class="mb-3 mt-5">
                  <label for="exampleInputEmail1" class="form-label">Enter your Username</label>
                  <input type="text" name="username" class="form-control" required>
                </div>
                <div class="mb-3">
                  <label for="Password" class="form-label">Enter your Password</label>
                  <input type="password" name="password" class="form-control" required>
                  <a style="text-decoration: none;" href=""><div id="emailHelp" style="color: #0d6efd;" class="form-text mt-3">Forget password ?</div></a>
                </div>
                <button type="submit" name="submit" class="btn btn-primary mt-5">Login</button>
                <div id="emailHelp" style="color:black; font-weight:600; text-align:center;" class="form-text mt-3 cus_mobile_issues">Don't Have an Account? <a style="text-decoration:none;" href="">Click Here</a></div>
              </form>
        </div>
    <!-- Option 1: Bootstrap Bundle with Popper -->
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
  </body>
</html>


这些是与登录相关的文件。我不是LaravelMaven,我如何解决这个问题。提前谢谢你

db2dz4w8

db2dz4w81#

Laravel中,Auth::attempt方法将尝试从Associative Array/Array of key-value pairsKeys中获取列名,但当您传递Array时,由于它只有index,它将得到索引而不是键,键应该是一个列名。这就是为什么列名变成0和错误弹出。
正如**@John Lobo提到的,$request->validate已经返回了一个Associative Array**,但是你通过 Package 它Auth::attempt([$credentials])将它转换为Array
你可以简单地传递$credentials而不 Package 它来解决这个问题。

if(Auth::attempt($credentials)){
    return view('welcome');
}

字符串

相关问题