0

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 App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\File; 


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
  • Welcome to SO ... what about this isn't working? what are you expecting to be happening? what about filled() isn't working? Commented Mar 6, 2023 at 23:06
  • Sorry @lagbox , I expected the database to update when just there is just one input, e.g. username. However, there needs to be a value in all three of the username, email and profile_descriptions for the database to update. Hope this is clearer. Commented Mar 6, 2023 at 23:11

2 Answers 2

0

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your response. The issue is that $request->filled('username') is for some reason evaluating to true. When all fields have a valid input, the database is updating fine so this is not the issue.
0

Update

Solution has been found, in the request validation, the key must have the rule 'nullable', e.g.

        $request->validate([
            'username' =>'nullable|min:4|unique:users,username|string|max:255',
            'email'=>'nullable|unique:users,email|email:filter|max:255',
            'profile_description'=>'nullable|string|max:10000',
            'file'=> 'max:10000'
        ]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.