-
composer.json
Open in GitHub{ // "require": { "php": "^7.3|^8.0", // "spatie/laravel-medialibrary": "^8.10" }, // }
-
resources/views/frontend/users/profileEdit.blade.php
Open in GitHub// {{ html()->modelForm($userprofile, 'PATCH', route('frontend.users.profileUpdate', $$module_name_singular->id))->class('form-horizontal')->acceptsFiles()->open() }} // <div class="col-md-5"> <div class="form-group"> <label for="file-multiple-input">Click here to update photo</label> <input id="file-multiple-input" name="avatar" multiple="" type="file" class="form-control-file"> </div> </div> // {{ html()->closeModelForm() }} //
-
app/Models/User.php
Open in GitHubuse App\Models\Traits\HasHashedMediaTrait; use Spatie\MediaLibrary\HasMedia; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable implements HasMedia { use HasHashedMediaTrait; // }
-
app/Models/Traits/HasHashedMediaTrait.php
Open in GitHubuse Spatie\MediaLibrary\InteractsWithMedia; use Spatie\MediaLibrary\MediaCollections\FileAdder; trait HasHashedMediaTrait { use InteractsWithMedia { InteractsWithMedia::addMedia as parentAddMedia; } public function addMedia($file): FileAdder { return $this->parentAddMedia($file)->usingFileName($file->hashName()); } }
-
app/Http/Controllers/Frontend/UserController.php
Open in GitHubuse App\Http\Controllers\Controller; use Illuminate\Http\Request; class UserController extends Controller { public function profileUpdate(Request $request, $id) { // if ($request->hasFile('avatar')) { if ($$module_name_singular->getMedia($module_name)->first()) { $$module_name_singular->getMedia($module_name)->first()->delete(); } $media = $$module_name_singular->addMedia($request->file('avatar'))->toMediaCollection($module_name); $$module_name_singular->avatar = $media->getUrl(); $$module_name_singular->save(); } // } // }