未配置数据库连接 [用户]

使用 Laravel Auth 提交注册表时,我收到InvalidArgumentException Database connection [users] not configured. http://127.0.0.1:8000/register错误。我的 Laravel 版本是 6.17.1


.env 文件


DB_CONNECTION=mysql

DB_HOST=127.0.0.1

DB_PORT=3306

DB_DATABASE=laravel

DB_USERNAME=root

DB_PASSWORD=

Auth/RegisterController.php 文件


<?php


namespace App\Http\Controllers\Auth;


use App\Http\Controllers\Controller;

use App\Providers\RouteServiceProvider;

use App\User;

use Illuminate\Foundation\Auth\RegistersUsers;

use Illuminate\Support\Facades\Hash;

use Illuminate\Support\Facades\Validator;


class RegisterController extends Controller

{

    /*

    |--------------------------------------------------------------------------

    | Register Controller

    |--------------------------------------------------------------------------

    |

    | This controller handles the registration of new users as well as their

    | validation and creation. By default this controller uses a trait to

    | provide this functionality without requiring any additional code.

    |

    */


    use RegistersUsers;


    /**

     * Where to redirect users after registration.

     *

     * @var string

     */

    protected $redirectTo = RouteServiceProvider::HOME;


    /**

     * Create a new controller instance.

     *

     * @return void

     */

    public function __construct()

    {

        $this->middleware('guest');

    }


    /**

     * Get a validator for an incoming registration request.

     *

     * @param  array  $data

     * @return \Illuminate\Contracts\Validation\Validator

     */

    protected function validator(array $data)

    {

        return Validator::make($data, [

            'name' => ['required', 'string', 'max:20'],

            'surname' => ['required', 'string', 'max:30'],

            'age' => ['required', 'integer', 'max:100'],

            'email' => ['required', 'string', 'email', 'max:255', 'unique:users.email'],

            'password' => ['required', 'string', 'min:6'],

            'confirm' => ['required', 'string', 'same:password']

        ]);

    }

}


ibeautiful
浏览 115回答 4
4回答

白猪掌柜的

我有同样的错误,它是由于错误的验证角色而发生的。我收到了这个错误:{"message": "Database connection [users] not configured.","exception": "InvalidArgumentException", ...&nbsp;}再次检查您的验证规则,尤其是在 [unique] 规则上。我的错误代码:$rules = [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'username'&nbsp; => 'required|min:8|unique:users.username,'.$request->input('user-id')];正确的:$rules = [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'username'&nbsp; => 'required|min:8|unique:users,username,'.$request->input('user-id')];您在“Auth/RegisterController.php”中有同样的问题:替换这个:protected function validator(array $data){&nbsp; &nbsp; return Validator::make($data, [&nbsp; &nbsp; &nbsp; &nbsp; 'name' => ['required', 'string', 'max:20'],&nbsp; &nbsp; &nbsp; &nbsp; 'surname' => ['required', 'string', 'max:30'],&nbsp; &nbsp; &nbsp; &nbsp; 'age' => ['required', 'integer', 'max:100'],&nbsp; &nbsp; &nbsp; &nbsp; 'email' => ['required', 'string', 'email', 'max:255', 'unique:users.email'],&nbsp; &nbsp; &nbsp; &nbsp; 'password' => ['required', 'string', 'min:6'],&nbsp; &nbsp; &nbsp; &nbsp; 'confirm' => ['required', 'string', 'same:password']&nbsp; &nbsp; ]);}有了这个 :&nbsp; &nbsp; protected function validator(array $data){&nbsp; &nbsp; return Validator::make($data, [&nbsp; &nbsp; &nbsp; &nbsp; 'name' => ['required', 'string', 'max:20'],&nbsp; &nbsp; &nbsp; &nbsp; 'surname' => ['required', 'string', 'max:30'],&nbsp; &nbsp; &nbsp; &nbsp; 'age' => ['required', 'integer', 'max:100'],&nbsp; &nbsp; &nbsp; &nbsp; 'email' => ['required', 'string', 'email', 'max:255', 'unique:users,email'],&nbsp; &nbsp; &nbsp; &nbsp; 'password' => ['required', 'string', 'min:6'],&nbsp; &nbsp; &nbsp; &nbsp; 'confirm' => ['required', 'string', 'same:password']&nbsp; &nbsp; ]);}

幕布斯6054654

我在 Laravel 8.x 上遇到了同样的错误,为了解决您的问题,您必须将电子邮件输入的验证规则从更改unique:users.email为unique:users,email:protected function validator(array $data){&nbsp; &nbsp; return Validator::make($data, [&nbsp; &nbsp; &nbsp; &nbsp; 'name' => ['required', 'string', 'max:20'],&nbsp; &nbsp; &nbsp; &nbsp; 'surname' => ['required', 'string', 'max:30'],&nbsp; &nbsp; &nbsp; &nbsp; 'age' => ['required', 'integer', 'max:100'],&nbsp; &nbsp; &nbsp; &nbsp; 'email' => ['required', 'string', 'email', 'max:255', 'unique:users,email'],&nbsp; &nbsp; &nbsp; &nbsp; 'password' => ['required', 'string', 'min:6'],&nbsp; &nbsp; &nbsp; &nbsp; 'confirm' => ['required', 'string', 'same:password']&nbsp; &nbsp; ]);}

慕桂英4014372

首先运行php artisan config:cache & php artisan cache:clear并重试。如果问题仍然存在,您可以在用户模型中手动指定数据库连接:<?phpnamespace App;use Illuminate\Contracts\Auth\MustVerifyEmail;use Illuminate\Foundation\Auth\User as Authenticatable;use Illuminate\Notifications\Notifiable;class User extends Authenticatable{&nbsp; &nbsp; use Notifiable;&nbsp; &nbsp; # database connection&nbsp; &nbsp; protected $connection = 'mysql';&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* The attributes that are mass assignable.&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @var array&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; protected $fillable = [&nbsp; &nbsp; &nbsp; &nbsp; 'name', 'surname', 'age', 'email', 'password', 'code', 'ban', 'ban_reason', 'created_at', 'image', 'role', 'confirmed'&nbsp; &nbsp; ];&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* The attributes that should be hidden for arrays.&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @var array&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; protected $hidden = [&nbsp; &nbsp; &nbsp; &nbsp; 'password', 'code', 'confirmed'&nbsp; &nbsp; ];&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* The attributes that should be cast to native types.&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @var array&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; protected $casts = [&nbsp; &nbsp; &nbsp; &nbsp; 'email_verified_at' => 'datetime',&nbsp; &nbsp; ];}通常这不是必需的,因为您的应用程序mysql用作默认连接,尽管我不知道您的环境。问题可能在任何地方。希望这可以帮助。

噜噜哒

尝试改变DB_CONNECTION=mysql至DB_CONNECTION=localhost
打开App,查看更多内容
随时随地看视频慕课网APP