-2
I need to update a field without changing the description, follow the image below.
I changed the field TYPE OF SERVICE but I have to keep the same description, how do I do that? NOTE: I am in a stage where I was asked to solve this problem, I am still a little layman.
Follow the controller update code:
public function update(Request $request, $id)
{
$item = Service::findOrFail($id);
Validator::make(
$request->all(),
$this->rules($request, $item->getKey())
)->validate();
$item->fill($request->all());
if ($request->hasFile('image') && $request->file('image')->isValid()) {
$upload = $request->image->store('services', 'public');
if ($item->icon) {
Storage::disk('public')->delete($item->icon);
}
$item->icon = $upload;
}
$item->save();
return redirect()->route('services.index')
->withStatus('Registro atualizado com sucesso.');
}
And the validation part:
private function rules(Request $request, $primaryKey = null, bool $changeMessages = false)
{
$rules = [
'description' => ['required', 'max:40', 'unique:services,description'],
'is_enabled' => ['required'],
'type_id' => ['required'],
'image' => 'nullable|image|mimes:jpeg,png,jpg,gif,svg|max:1024',
];
$messages = [
'description.unique' => 'Descrição já existe!',
];
return !$changeMessages ? $rules : $messages;
}