dsgnze6572 2018-09-20 16:29
浏览 391

类型错误:传递给Illuminate \ Auth \ SessionGuard :: login()重定向的参数1在寄存器上不起作用

I have this on my controller but the redirection does not work it says Type error: Argument 1 passed to Illuminate\Auth\SessionGuard::login(), I have tried this so far but nothing

<?php

namespace App\Http\Controllers\Auth;

use App\Events\NewUserWasRegistered;
use App\Http\Controllers\Controller;
use App\Models\User;
use Illuminate\Foundation\Auth\RegistersUsers;
use Validator;
use Auth;

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 login / registration.
     *
     * @var string
     */
    protected $redirectTo = '/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)
    {
        $rules = [
                'email'                => 'required|email|max:255|unique:users',
                'password'             => 'required|confirmed|min:6',
                'g-recaptcha-response' => 'required|captcha',
                'allow_register'       => 'required|accepted',
            ];

        if (app()->environment('local') || app()->environment('testing')) {
            unset($rules['g-recaptcha-response']);
        }

        $data['allow_register'] = config('root.app.allow_register', true);

        $messages = [
            'allow_register.accepted' => trans('app.allow_register'),
        ];

        return Validator::make($data, $rules, $messages);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     *
     * @return User
     */
    protected function create(array $data)
    {

        $user = User::create([
            'username' => md5("{$data['name']}/{$data['email']}"),
            'name'     => $data['name'],
            'email'    => $data['email'],
            'password' => bcrypt($data['password']),
            'role_id'     => $data['role'],

        ]);


        event(new NewUserWasRegistered($user));
          if($user){

                if ($user->role_id == 0) {

                   Auth::login($user);
                    return redirect('user/businesses');


                }else if($user->role_id == 1){

                    Auth::login($user);
                    return 'Working on..';
                }
            }

        // return $user;
    } 
}

Can someone please help me I am stuck, I have tried so many things but I am new on coding so maybe you will understand If it is that I am asking too much

  • 写回答

1条回答 默认 最新

  • drcigvoy48900 2018-09-20 20:09
    关注

    App\Models\User must implement the interface/contract Illuminate\Contracts\Auth\Authenticatable.

    "If you need to log an existing user instance into your application, you may call the login method with the user instance. The given object must be an implementation of the Illuminate\Contracts\Auth\Authenticatable contract. Of course, the App\User model included with Laravel already implements this interface ..."

    Laravel 5.7 Docs - Authentication - Manually Authenticating Users - Other Authentication Methods

    Side Note:

    The create method of that controller is only for taking an array of data and creating a new User and returning that User instance. Nothing else. The registration flow out of the box calls that method, only to create the user. It then does the login and everything else.

    评论

报告相同问题?