Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

moctarss's avatar

How to properly save image path?

I am trying to save an image path on my project by doing this on my Controller:

public function storeImage(Employee $employee){
    dd(request()->person_image);
    $person = \App\Models\Person::find($employee->person->id);
    if(request()->has('person_image')){
        $person->update([
             'person_image' => request()->person_image->store('uploads/'.$person->id, 'public'),
        ]);
    }
}

and on my form i made this:

<form action="/employee" name="add-form" id="add-form" method="POST" enctype="multipart/form-data">
    <input type="file" name="person_image">
</form>

My problem now is upon saving into my database the person_image column value become C:\wamp\tmp\phpE913.tmp and as i use die and dump the output gives me this:

Illuminate\Http\UploadedFile {#315 ▼
  -test: false
  -originalName: "avatar-2.png"
  -mimeType: "image/png"
  -error: 0
  #hashName: null
  path: "C:\wamp\tmp"
  filename: "php9B90.tmp"
  basename: "php9B90.tmp"
  pathname: "C:\wamp\tmp\php9B90.tmp"
  extension: "tmp"
  realPath: "C:\wamp\tmp\php9B90.tmp"
  aTime: 2020-03-29 10:15:29
  mTime: 2020-03-29 10:15:29
  cTime: 2020-03-29 10:15:29
  inode: 0
  size: 81691
  perms: 0100666
  owner: 0
  group: 0
  type: "file"
  writable: true
  readable: true
  executable: false
  file: true
  dir: false
  link: false
  linkTarget: "C:\wamp\tmp\php9B90.tmp"
}

What mistake i did?

0 likes
10 replies
Snapey's avatar

looks ok, but we only see part of your code. I would guess somewhere else, after, you are updating Person with request->all()

moctarss's avatar

Sir @snapey I just call my method storeImage() in my update(Request $request, Employee $employee) method, something like this:

\App\Models\Person::where('id', $employee->person_id)->update($validator->validated());
$this->storeImage($employee);
Snapey's avatar

So, you overwrote person_image on your model with the validated data?

what is $employee?

moctarss's avatar

@snapey $employee is my Model that has One-to-One relationship to my Person model. So in my storeImage method: i used $person = \App\Models\Person::find($employee->person->id); to retrieve my person data.

Snapey's avatar
Snapey
Best Answer
Level 122

does that work? Perhaps findOrFail might be better.

Your save image function looks ok, however it needlessly refetches the person when you have already done that.

It should update the model with the new data but it may be overwritten later.

I would instead, something like this:

$person = $employee->person()->get();
$person->fill($validator->validated());
$person->person_image = $this->storeImage($person);
$person->save();

and then

public function storeImage(Person $person){
    
    if(! request()->has('person_image')){
	return $person->person_image;
    }

    return request()->person_image->store('uploads/'.$person->id, 'public');

}
moctarss's avatar

Sir @snapey it says Method Illuminate\Database\Eloquent\Collection::fill does not exist.. Is fill a predefined method in Eloquent?

Snapey's avatar

fill is an eloquent model method.

My example is wrong, it should be ->first() not get(). get returns a collection

$person = $employee->person()->first();

moctarss's avatar

Thank you very much sir @snapey it now work. But i cannot understand why before, it saves different string into my database? Does it mean that i override the value of my person_image when i re fetch my model? Nonetheless, thank you very much.

Snapey's avatar

no, it will not be overwritten. If you don't supply a new image, it returns whatever the value is currently, so no change.

Please or to participate in this conversation.