Redundant (and likely inefficient) code
There are multiple blocks that repeat:
if($username){ $user->username = $username; $user->save(); } if($wallet_address){ $user->wallet_address = $wallet_address; $user->save(); } if($about){ $user->about = $about; $user->save(); } if($password){ $user->password = $password; $user->save(); } if($website){ $user->website = $website; $user->save(); } if($location){ $user->location = $location; $user->save(); }
The only thing that really changes here is the property/field being checked. That could be simplified using an array containing the field names:
//could be moved to the model or another namespace
const FIELDS = ['username', 'wallet_address', 'about', 'password', 'website', 'location'];
public function update(Request $request){
$user = Auth::user();
foreach (self::FIELDS as $field) {
if ($request->$field) {
$user->$field = $request->$field;
$user->save();
}
}
Obviously that shows that the save() method is called multiple times, which could be inefficient if it has to write data (e.g. to a database or other data store). You could conditionally call the save method if one of those fields has a value and is updated on the user. The save method is also called when an image is included in the request (i.e. when $request->file('avatar') has a non-null value). That boolean could be combined with that condition as well, which would lead to a single point of updating the user model.
Validation of the Request
You could create a Request subclass to validate the fields that should be (sometimes) present on the request
For example, you could run
php artisan make:request StoreUserRequest
And that would create StoreUserRequest in the app/Http/Requests directory which would extend Illuminate\Foundation\Http\FormRequest. The boilerplate would give it an empty rules() method that could contain the fields that can be passed. For Example:
public function rules()
{
return [
'username' => 'sometimes',
'wallet_address' => 'sometimes',
'about' => 'sometimes',
'password' => 'sometimes',
'location' => 'sometimes',
];
}
And those can container other rules - for example, if a field is required, has a maximum string length, etc.
Then in the controller method use that new request subclass as the type for $request.