looks ok, but we only see part of your code. I would guess somewhere else, after, you are updating Person with request->all()
Mar 29, 2020
10
Level 1
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?
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');
}
Please or to participate in this conversation.