How to update unique field?

Hello, in the controller when editing a product doing so
public function editRequestProduct(Request $request, int $id){
 $validator = Validator::make($request->all(), [
 'title' => 'required|string|min:4|max:25',
 'slug' => array(
'required',
'string',
'min:4',
'max:25',
'unique:products',
'regex:/^[a-z0-9a-jaja-]+$/u',
),
 'excerpt' => 'required|string|min:4|max:100',
 'content' => 'required|string|min:4|max:300',
 'descrtitle' => 'required|string|min:4|max:100',
 'descrtext' => 'required|string|min:4|max:300',
 'descr' => 'required|string|min:4|max:300',
 'regular_price' => array(
'required',
'regex:/\d+/',
),
 'discount' => array(
'required',
'min:0',
'max:100',
'regex:/^\d+(\.\d{1,2})?$/',
),
 'image' => 'mimes:jpeg,jpg,png,gif|max:10000',
 'tab_bg' => 'mimes:jpeg,jpg,png,gif|max:10000',
]);

if($validator->fails()){
 return back()->withErrors($validator)->withInput();
}

 $objProducts = Products::find($id);
if(!$objProducts){
 return abort('404');
}

 $is_reccomended = $request->has('is_reccomended') ? true : false;

 $image = ImageDNK::save($request, 'image');
 $tab_bg = ImageDNK::save($request, 'tab_bg');


 $objProducts->title = $request->input('title');
 $objProducts->slug = $request->input('slug');
 $objProducts->excerpt = $request->input('excerpt');
 $objProducts->content = $request->input('content');
 $objProducts->descrtitle = $request->input('descrtitle');
 $objProducts->descrtext = $request->input('descrtext');
 $objProducts->descr = $request->input('descr');
 $objProducts->regular_price = $request->input('regular_price');
 $objProducts->sale_price = $request->input('sale_price');
 $objProducts->discount = $request->input('discount');
 $objProducts->currency = $request->input('currency');
 $objProducts->image = $image;
 $objProducts->is_reccomended = $is_reccomended;
 $objProducts->product_description_tab_content = $request->input('product_description_tab_content');
 $objProducts->product_ingredients_tab_content = $request->input('product_ingredients_tab_content');
 $objProducts->product_usage_tab_content = $request->input('product_usage_tab_content');
 $objProducts->tab_bg = $tab_bg;

if(!$objProducts->save()){
 return back()->with('error', 'the Product has not changed. Try again');
}

 $hasCategory = $request->input('product_category') != 0;

if($hasCategory){
 $objCatsRels = new CategoriesRelationship();
 $objCatsRels = $objCatsRels->create([
 'object_id' => $id,
 'category_id' => $request->input('product_category'),
]);
}

 if($objProducts && (!$hasCategory || $objCatsRels)){
 return back()->with('success', trans('messages.products.successUpdated'));
}

 }

If you do not remove check for slug uniqueness of the existing slug will not pass the test. How to change the slug but check that other goods are not of such a slug?

Thank you.
March 23rd 20 at 20:20
1 answer
March 23rd 20 at 20:22
So. The question is easily solved lagu unique:table,id_to_ignore.
All the mess that you have written is solved in two rows.
$validatedFields = $request->validate($rules);
$model->fill($validatedFields);
Doesn't work? You need to use Validator::make().
$validator = Validator::make($request->all(), [
 'title' => 'required|string|min:4|max:25',
 'slug' => array(
'required',
'string',
'min:4',
'max:25',
'unique:products',
'regex:/^[a-z0-9a-jaja-]+$/u',
),
 'excerpt' => 'required|string|min:4|max:100',
 'content' => 'required|string|min:4|max:300',
 'descrtitle' => 'required|string|min:4|max:100',
 'descrtext' => 'required|string|min:4|max:300',
 'descr' => 'required|string|min:4|max:300',
 'regular_price' => array(
'required',
'regex:/\d+/',
),
 'discount' => array(
'required',
'min:0',
'max:100',
'regex:/^\d+(\.\d{1,2})?$/',
),
 'image' => 'mimes:jpeg,jpg,png,gif|max:10000',
 'tab_bg' => 'mimes:jpeg,jpg,png,gif|max:10000',
]);

$model->fill($validator);
- Dixie commented on March 23rd 20 at 20:25
@Dixie, you don't need to use php, because he's got such a bad name. - andreane_Windler commented on March 23rd 20 at 20:28
@Dixie, you need to use a FormRequest for validation, and not clutter up the controller. - Ernestina_Purdy73 commented on March 23rd 20 at 20:31
@andreane_Windler, Yes, and such instead of giving constructive criticism just saying that I am a novice to php. I know this is true. I'm not an expert in php. That's why I'm asking the questions here. - Dixie commented on March 23rd 20 at 20:34
@Dixie, instead of having to ask questions, maybe you should learn php?

In this instance, Laravel, this is in the documentation. But you and it will not help, because the answer you gave, but you are not able to perceive.

https://laravel.com/docs/5.8/validation - andreane_Windler commented on March 23rd 20 at 20:37
@jacinto ,
$validatedFields = $request->validate($rules);
$model->fill($validatedFields);

Here at $validatedFields get only those fields in the rules array. What about a checkbox which is not validinputs? Add it to $validatedFields? - Dixie commented on March 23rd 20 at 20:40

Find more questions by tags Laravel