Search
 
SCRIPT & CODE EXAMPLE
 

PHP

update profile method in laravel

//web.php
Route::post('update-profile',[AppHttpControllersHomeController::class,'updateProfile'])->name('update-profile');


//_controller.php

public function __construct()
    {
        $this->middleware('auth');
    }

public function updateProfile(Request $request){

       $request->validate([
            "name" => "required|min:3|max:50",
            "photo" => "nullable|file|mimes:jpeg,png|max:1000",
        ]);

        $user = User::find(auth()->id());
        $user->name = $request->name;
        if($request->hasFile('photo')){

//          delete old photo  => path:storage/profile/profile_6221cecf36ad9.jpg
            $subStrPhotoName = Str::substr($user->photo,16);
            Storage::delete('public/profile/'.$subStrPhotoName);

//            create new photo
            $dir="storage/profile";
            $newName = "profile_".uniqid().".".$request->file('photo')->extension();
            $request->file('photo')->storeAs("public/profile",$newName);
            $user->photo = $dir."/".$newName;
        }
        $user->update();
        return redirect()->back();
    }


//update.blade.php
<form action="{{ route('update-profile') }}" method="post" enctype="multipart/form-data">
                        @csrf
                        <div class="">
                            <input type="file" name="photo" accept="image/jpeg,image/png" value="{{ old('photo',auth()->user()->photo) }}" class="@error('photo') is-invalid @enderror">
                            @error('photo')
                            <div class="invalid-feedback ps-2">{{ $message }}</div>
                            @enderror
                        </div>
                        <div class="form-floating mb-3">
                            <input type="text" name="name" class="form-control @error('name') is-invalid @enderror" id="yourName" value="{{ auth()->user()->name }}" placeholder="name@example.com">
                            <label for="yourName">Your Name</label>
                            @error('name')
                            <div class="invalid-feedback ps-2">{{ $message }}</div>
                            @enderror
                        </div>
                        <div class="form-floating mb-3">
                            <input disabled type="email"  class="form-control" id="yourEmail" value="{{ auth()->user()->email }}" placeholder="name@example.com">
                            <label for="yourEmail">Your Email</label>
                        </div>

                        <div class="text-center">
                            <button class="btn btn-lg btn-primary text-white">Update Profile</button>
                        </div>
                    </form>
Comment

PREVIOUS NEXT
Code Example
Php :: php api method post 
Php :: laravel unique validation on multiple columns 
Php :: php ?? 
Php :: is serialized php 
Php :: laravel resource set status code 
Php :: laravel activity log package 
Php :: laravel validation custom message 
Php :: how to get attachments to emails php 
Php :: convert Persian/Arabic numbers to English numbers PHP 
Php :: find days with name between two dates in php 
Php :: wordpress get category description 
Php :: laravel collective form include image 
Php :: if else if php code reflect 
Php :: Eloquent where date methods 
Php :: laravel collection average 
Php :: set posts_per_page 
Php :: pdf to image php 
Php :: doctrine querybuilder print sql 
Php :: how to convert youtube url to embed code in php 
Php :: php datetime from timestamp 
Php :: move uploaded file in php 
Php :: phpmailer addattachment 
Php :: php foreach array pop 
Php :: laravel app running in console 
Php :: multi theme laravel 
Php :: laravel the requested url was not found on this server 
Php :: woocommerce get the price from session after add to cart 
Php :: php add custom button in wordpress editor 
Php :: softDelete laravel8 
Php :: Undefined constant "STDOUT" in php 
ADD CONTENT
Topic
Content
Source link
Name
9+6 =