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

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

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


的数据

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

  1. namespace App\Http\Controllers;
  2. use Illuminate\Http\Request;
  3. use Illuminate\Support\Facades\Auth;
  4. use Hash;
  5. use App\Models\Customer;
  6. class LoginController extends Controller
  7. {
  8. public function login(){
  9. return view('user.login');
  10. }
  11. public function check(Request $request)
  12. {
  13. $credentials= $request->validate([
  14. 'username' => ['required'],
  15. 'password' => ['required'],
  16. ]);
  17. if(Auth::attempt([$credentials])){
  18. return view('welcome');
  19. }
  20. else{
  21. return view('unauth');
  22. }
  23. }
  24. }

字符串

Customer.php模型如下所示

  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Factories\HasFactory;
  4. use Illuminate\Database\Eloquent\Model;
  5. class Customer extends Model
  6. {
  7. use HasFactory;
  8. protected $fillable=[
  9. 'name',
  10. 'username',
  11. 'password',
  12. 'email',
  13. 'invoice',
  14. 'kwh',
  15. 'warranty',
  16. 'date'
  17. ];
  18. }

web.php中的代码如下所示

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

Login.blade.php代码如下

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <!-- Required meta tags -->
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1">
  7. <!-- Bootstrap CSS -->
  8. <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
  9. <link href="{{asset('css/customs.css')}}" rel="stylesheet" >
  10. <title>Login Form</title>
  11. </head>
  12. <body style=" background-image: url({{url('img/img-1.webp')}});">
  13. <div class="container-fluid">
  14. <form action="{{ route('check') }}" method="POST" class="mx-auto formlogin">
  15. {!! csrf_field() !!}
  16. <div style="text-align: center;">
  17. <img src="{{url('img/logo.webp')}}" alt="logo" width="170px;">
  18. </div>
  19. <div class="mb-3 mt-5">
  20. <label for="exampleInputEmail1" class="form-label">Enter your Username</label>
  21. <input type="text" name="username" class="form-control" required>
  22. </div>
  23. <div class="mb-3">
  24. <label for="Password" class="form-label">Enter your Password</label>
  25. <input type="password" name="password" class="form-control" required>
  26. <a style="text-decoration: none;" href=""><div id="emailHelp" style="color: #0d6efd;" class="form-text mt-3">Forget password ?</div></a>
  27. </div>
  28. <button type="submit" name="submit" class="btn btn-primary mt-5">Login</button>
  29. <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>
  30. </form>
  31. </div>
  32. <!-- Option 1: Bootstrap Bundle with Popper -->
  33. <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
  34. </body>
  35. </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 它来解决这个问题。

  1. if(Auth::attempt($credentials)){
  2. return view('welcome');
  3. }

字符串

相关问题