You can use nullable in your validation rule.
Make sure to only update the value if a file is present.
In my project, when I updating the other fields the image upload validation makes a problem. Basically, I want simply update the input values and the Image is updated when if the file is selected but I write the condition but it not working.
Can anyone please look check my code and tells me what I did wrong? This is update method
public function logo_update($id)
{
$validatedData = $this->validate([
'name' => 'required',
'job_title' => 'required',
'facebook_url' => 'required',
'linkedin_url' => 'required',
'insta_url' => 'required',
'twitter_url' => 'required',
'logo' => 'image|max:512|mimes:png,jpg,jpeg',
]);
$row_data = ThemeTeam::find($id);
if ($this->logo) {
$validatedData['logo'] = $this->logo->storePublicly('client-logo','public');
}
$row_data->update($validatedData);
session()->flash('message', 'Client Logo updated successfully');
$this->iteration++;
$this->reset();
}
and this is blade file
<div class="card">
@if(!$table_not_found)
<div class="card-header text-primary font-weight-bold">Teams</div>
<div class="card-body">
@if($wants_to_update)
<form wire:submit.prevent="logo_update({{ $rowId }})">
@else
<form wire:submit.prevent="store">
@endif
<div class="row">
<div class="col-12">
<div class="row">
<div class="col-4">
<div class="form-group">
<label for="name">Name</label>
<input wire:model="name" type="text" class="form-control" id="name"
aria-describedby="emailHelp">
@error('name')
<p class="text-danger">{{ $message }}</p>
@enderror
</div>
</div>
<div class="col-4">
<div class="form-group">
<label for="job_title">Job Title</label>
<input wire:model="job_title" type="text" class="form-control" id="job_title"
aria-describedby="emailHelp">
@error('job_title')
<p class="text-danger">{{ $message }}</p>
@enderror
</div>
</div>
<div class="col-4">
<div class="form-group">
<label for="facebook_url">Facebook Url</label>
<input wire:model="facebook_url" type="text" class="form-control" id="facebook_url"
aria-describedby="emailHelp">
@error('facebook_url')
<p class="text-danger">{{ $message }}</p>
@enderror
</div>
</div>
<div class="col-4">
<div class="form-group">
<label for="linkedin_url">LinkedIn Url</label>
<input wire:model="linkedin_url" type="text" class="form-control" id="linkedin_url"
aria-describedby="emailHelp">
@error('linkedin_url')
<p class="text-danger">{{ $message }}</p>
@enderror
</div>
</div>
<div class="col-4">
<div class="form-group">
<label for="twitter_url">Twitter Url</label>
<input wire:model="twitter_url" type="text" class="form-control" id="twitter_url"
aria-describedby="emailHelp">
@error('twitter_url')
<p class="text-danger">{{ $message }}</p>
@enderror
</div>
</div>
<div class="col-4">
<div class="form-group">
<label for="insta_url">Instagram Url</label>
<input wire:model="insta_url" type="text" class="form-control" id="insta_url"
aria-describedby="emailHelp">
@error('insta_url')
<p class="text-danger">{{ $message }}</p>
@enderror
</div>
</div>
<div class="col-4">
<div class="form-group">
<label for="logo">Logo</label>
<input wire:model="logo" type="file" class="form-control-file"
id="logo{{ $iteration }}">
<small id="emailHelp" class="form-text text-muted">JPEG, PNG
only</small>
@error('logo')
<p class="text-danger">{{ $message }}</p>
@enderror
</div>
</div>
@if($previous_image)
<div class="col-4">
<img src="{{ asset('storage/'.$this->previous_image) }}"
width="75%" height="75%">
</div>
@endif
</div>
</div>
<div class="col-12">
<div class="text-right">
<button wire:click="resetAll" type="submit" class="btn btn-outline-secondary">
Cancel
</button>
<button type="submit" class="btn btn-primary">
{{ ($wants_to_update) ? 'Update' : 'Save' }}
</button>
</div>
</div>
</div>
</form>
</div>
@endif
</div>
You can also easily unset it :)
if ($this->logo) {
$validatedData['logo'] = $this->logo->storePublicly('client-logo','public');
} else {
unset($validatedData['logo']);
}
Please or to participate in this conversation.