I am working on a function to update profile details, in my ProfileController.php. I am validating the request, making sure nothing is required, however when I use the filled() function:
if($request->filled('username')){
$user->username = $request['username'];
}
if($request->filled('email')){
$user->email = $request['email'];
}
if($request->filled('profile_description')){
$user->profile_description = $request['profile_description'];
}
This does not work, and the database will only update when all fields have been filled, except for the profile picture:
if($request->hasFile('file')){
if ($request['file']->isValid()) {
$file = $request['file'];
$destination = 'images/profile_pictures'.'/';
$ext= $file->getClientOriginalExtension();
$mainFilename = $user->username;
$user->pfp_file_extension = $ext;
// check if user has existing pfp
if (File::exists($destination, $mainFilename.".".$user->pfp_file_extension)) {
File::delete($destination, $mainFilename.".".$user->pfp_file_extension);
}
$file->move($destination, $mainFilename.".".$ext);
}
}
For some reason hasFile() is working as it allows me to not upload a picture.
I’ve read the Laravel documentation and it states "The field under validation must not be empty when it is present", which does not explain why it is not working. Here is my HTML form input.
<form method="POST" enctype="multipart/form-data">
@csrf
<h6 class="heading-small text-muted mb-4">User information</h6>
<div class="pl-lg-4">
<div class="row">
<div class="col-lg-6">
<div class="form-group focused">
<label class="form-control-label">Username</label>
<input type="text" id="username" name="username" class="form-control form-control-alternative" placeholder="{{ Auth::user()->username }}">
</div>
</div>
<div class="col-lg-6">
<div class="form-group">
<label class="form-control-label">Email address</label>
<input type="email" id="email" name="email" class="form-control form-control-alternative" placeholder="{{ Auth::user()->email }}">
</div>
</div>
<div class="col-lg-6">
<div class="form-group focused">
<label class="form-control-label">Profile Picture</label>
<input type="file" class="file" name="file" id="file" accept=".png, .jpg, .jpeg">
</div>
</div>
<div class="row">
</div>
</div>
<hr class="my-4">
<!-- Description -->
<h6 class="heading-small text-muted mb-4">About me</h6>
<div class="pl-lg-4">
<div class="form-group focused">
<label>About Me</label>
<textarea id="profile_description" name="profile_description" rows="4" class="form-control form-control-alternative" placeholder="{{ Auth::user()->profile_description }}"></textarea>
</div>
</div>
<div>
<button class="btn btn-sm btn-primary" type="submit">Update Profile</button>
</div>
</form>
Also, here is the entirety of my ProfileController.php.
<?php
namespace AppHttpControllers;
use IlluminateHttpRequest;
use IlluminateSupportFacadesAuth;
use IlluminateSupportFacadesFile;
class ProfileController extends Controller
{
public function index()
{
return view('profile');
}
public function profileUpdate(Request $request){
//validation rules
$request->validate([
'username' =>'min:4|unique:users,username|string|max:255',
'email'=>'unique:users,email|email:filter|max:255',
'profile_description'=>'string|max:10000',
'file'=> 'max:10000'
]);
$user = Auth::user();
if($request->filled('username')){
$user->username = $request['username'];
}
if($request->filled('email')){
$user->email = $request['email'];
}
if($request->filled('profile_description')){
$user->profile_description = $request['profile_description'];
}
if($request->hasFile('file')){
if ($request['file']->isValid()) {
$file = $request['file'];
$destination = 'images/profile_pictures'.'/';
$ext= $file->getClientOriginalExtension();
$mainFilename = $user->username;
$user->pfp_file_extension = $ext;
// check if user has existing pfp
if (File::exists($destination, $mainFilename.".".$user->pfp_file_extension)) {
File::delete($destination, $mainFilename.".".$user->pfp_file_extension);
}
$file->move($destination, $mainFilename.".".$ext);
}
}
$user->save();
return back()->with('message','Profile Updated');
}
}
Thank you in advance.
2
Answers
Update
Solution has been found, in the request validation, the key must have the rule 'nullable', e.g.
It’s because you should not get the properties of the
$request
variable in this way like it’s a common array or something like that.The
$request->fill
is probably working, but the attribution isn’t.You should do this way:
$user->email = $request->input('email');
For more info, check the DOCS: https://laravel.com/docs/10.x/requests#retrieving-an-input-value