【问题标题】:using laravel auth with different table将 laravel auth 与不同的表一起使用
【发布时间】:2020-01-07 18:17:19
【问题描述】:

如前所述,我想使用不同的表(客户端)进行身份验证。

我已经编辑了一些代码,但是我仍然无法使用auth方法。

我尝试了太多变种,但在用户注册后仍然无法使用身份验证登录。

config/auth.php

    'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],
        'client' => [
            'driver' => 'session',
            'provider' => 'clients',
        ]


    'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\User::class,
        ],
        'clients' => [
            'driver' => 'eloquent',
            'model' => App\Client::class,
        ],

客户端模式文件。

    class Client extends Authenticatable
    {

        protected $guard = 'client';


        public $timestamps = true;

        protected $fillable = [
            'email',
            'password',
            'fullname',
        ];


        protected $hidden = [
            'password', 'remember_token',
        ];

    }

客户端迁移文件。

    Schema::create('clients', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('email')->unique();
        $table->string('password');
        $table->string('fullname');
        $table->rememberToken();
        $table->timestamps();
    });

控制器

    public function showRegister()
    {
        return view('pages.register');
    }

    public function doRegister(ClientRequest $request)
    {
        $validated = $request->validated();
        $request->merge(['created_at' => Carbon::now()]);
        $request->merge(['password' => Hash::make($request->password) ]);

        $add = Client::create($validated);

        auth('client')->attempt($add);

        return redirect('my_profile')->with('success', 'success');
    }

提交注册表后出现此错误。

    Symfony\Component\Debug\Exception\FatalThrowableError
    Argument 1 passed to Illuminate\Auth\SessionGuard::attempt() must be of the type array, object given, called in C:\wamp64\www\laravel\app\Http\Controllers\HomepageController.php on line 117

当我像这样更改我的尝试代码时,它返回 null。

    auth('client')->attempt([
        'email'=> $request->email,
        'password'=> $request->password
    ]);

【问题讨论】:

  • 您的用户是否在表格中成功注册?
  • 是的,先生,没有问题。

标签: laravel laravel-6


【解决方案1】:

如果user 已成功创建,请尝试此行。

auth('client')->login($add);

删除这一行

auth('client')->attempt($add);

【讨论】:

    猜你喜欢
    • 2016-09-13
    • 1970-01-01
    • 2021-09-03
    • 2013-01-14
    • 2021-05-17
    • 1970-01-01
    • 1970-01-01
    • 2018-12-17
    • 2013-09-23
    相关资源
    最近更新 更多