Laravel Form Request unique field validation on update

Viewed 32

I have make Form Request and have unique rule on title. how to ignore unique validate for updating id.

This is my rules where i trying to get id passed from controller.

public function rules()
    {
        $id = $this->request->get('id') ? ',' . $this->request->get('id') : '';
        $rules = [
            'title' => 'required|min:3|unique:parent_categories',
        ];
        if ($this->getMethod() == 'PUT' || $this->getMethod() == 'PATCH') {
            $rules += ['title' => 'required|min:3|unique:parent_categories,title' . $id];
        }

        return $rules;

    }

this my controller where i am trying to update content with id

public function update(ParentCategoryRequest $request, $id)
    {

        DB::beginTransaction();
        try {
            $parentCategory = ParentCategory::update($id, $request->all());
            DB::commit();
            return $this->success('Parent category updated successfully', new ParentCategoryResource($parentCategory), 201);

        } catch (Exception $e) {
            DB::rollBack();
            return $this->error($e->getMessage(), $e->getCode());
        }

    }

I am getting undefine variable $id on ParentCategoryRequest
0 Answers
Related