Whenever I try to only save profile changes for the profile description and url in the edit form, I get an error because I didn’t choose an image file also.
I would like to be able to update a profile with current image when an image file is not chosen in the edit form.
The error I keep getting is: Call to a member function store() on null
…that error is referring to this line in the update method of my UserController:
$imagePath = request('image')->store('uploads', 'public');
This is the entire update method in my UserController:
public function update(User $user, Request $request)
{
$data = request()->validate([
'description' => 'nullable',
'url' => 'nullable',
'image' => 'nullable',
]);
$imagePath = request('image')->store('uploads', 'public');
auth()->user()->profile()->update([
'description' => $data['description'],
'url' => $data['url'],
'image' => $imagePath,
]);
return redirect('/users/' . auth()->user()->id);
}
Finally, this is the form in my edit-profile.blade.php file:
@section('content')
<body class="home_body">
<div class="home_container_div">
<div class="home_container">
<div class="home_box_div">
<form action="{{('/users/' . auth()->user()->id)}}" enctype="multipart/form-data" method="post">
@csrf
@method('PATCH')
<div class="form-group">
<label for="description" class="edit_description_label">Description</label>
<div class="edit_description_div">
<input id="description"
type="text"
class="form-control @error('description') is-invalid @enderror"
name="description"
value="{{ old('description' ) ?? auth()->user()->profile->description }}"
autocomplete="description" autofocus>
@error('description')
<div class="invalid-feedback-div">
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
</div>
@enderror
</div>
</div>
<div class="form-group">
<label for="url" class="edit_title_label">URL</label>
<div class="edit_url_div">
<input id="url"
type="text"
class="form-control @error('url') is-invalid @enderror"
name="url"
value="{{ old('url' ) ?? auth()->user()->profile->url }}"
autocomplete="url" autofocus>
@error('url')
<div class="invalid-feedback-div">
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
</div>
@enderror
</div>
</div>
<div class="create_post_image_div">
<label for="image" class="create_image_label">Profile Image</label>
<input type="file" class="form-control-file" id="image" name="image">
@error('image')
<div class="invalid-feedback-div">
<strong>{{ $message }}</strong>
</div>
@enderror
<div class="create_post_btn_div">
<button class="create_post_btn">Save Profile</button>
</div>
</div>
</form>
</div>
</div>
</div>
</body>
@endsection
How can I resolve this issue?
Advertisement
Answer
You can skip the image upload part if the user has not selected any image file, like this:
public function update(User $user, Request $request)
{
$data = request()->validate([
'description' => 'required',
'url' => 'required',
'image' => 'nullable',
]);
$updateData = [
'description' => $data['description'],
'url' => $data['url'],
];
if (request('image')) {
$imagePath = request('image')->store('uploads', 'public');
$updateData['image'] = $imagePath;
}
auth()->user()->profile()->update($updateData);
return redirect('/users/' . auth()->user()->id);
}