rafidAhsan's avatar

Laravel jetstream image upload

I can work with object for uploading image but i don't know how to upload image with this kind of input array.

public function create(array $input)
    {
        Validator::make($input, [
            'name' => ['required', 'string', 'max:255'],
            'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
            'image' => 'required',
            'password' => $this->passwordRules(),
            'terms' => Jetstream::hasTermsAndPrivacyPolicyFeature() ? ['required', 'accepted'] : '',
        ])->validate();

        return DB::transaction(function () use ($input) {


            return tap(User::create([
                'name' => $input['name'],
                'email' => $input['email'],
                'password' => Hash::make($input['password']),
            ]), function (User $user) {
                $this->createTeam($user);
            });
        });
0 likes
2 replies
thinkverse's avatar
Level 15

Jetstream has profile image uploads built-in and has defaults already included. If you want that functionality to work on registration you could call the built-in functionality after the $this->createTeam($user) call, or right before it. Just do it after the user is created so you can use the updateProfilePhoto method.

return tap(User::create([
    'name' => $input['name'],
    'email' => $input['email'],
    'password' => Hash::make($input['password']),
]), function (User $user) use ($input) {
    $this->createTeam($user);

    if (isset($input['image'])) {
        $user->updateProfilePhoto($input['image']);
    }
});

This is how Fortify implements it in the UpdateUserProfileInformation class, you can look at the action under app/Actions/Fortify.

Please or to participate in this conversation.