Skip to content
Advertisement

Laravel how to register a user with a default image in user::create

I wanted to make it that when you register a new user it comes with a default picture :

https://upload.wikimedia.org/wikipedia/commons/8/89/Portrait_Placeholder.png

I tried something like this but it doesn’t work the image doesn’t stays in the database, how would I do it?

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 = "/registando";

    /**
     * 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 IlluminateContractsValidationValidator
     */
    protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => ['required', 'string', 'max:255'],
            'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
            'password' => ['required', 'string', 'min:8', 'confirmed'],
        ]);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return AppUser
     */
    protected function create(array $data)
    {
        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => Hash::make($data['password']),
            'profile_image' => 'https://upload.wikimedia.org/wikipedia/commons/8/89/Portrait_Placeholder.png'],
            
        ]);
    }
}

Advertisement

Answer

Firstly you have to change your User model and add the profile_image to fillable array:

protected $fillable = [
        'name', 'email', 'password', 'profile_image'
    ];
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement