Laravel validate unique with postgresql error array_map(): Argument #2 should be an array

Controller:

public function store(CatalogRequest $request, $collectionId)
    {
        $input = $request->all();
        $input['collection_id'] = $collectionId;
        $model = $this->model->create($input);

        return redirect(cms_route('catalog.edit', [$collectionId, $model->id]))
            ->with('alert', fill_data('success', trans('general.created')));
    }

Request:

public function rules()
    {
        $id = $this->route('catalog');
        return [
            'title' => 'required|min:2',
            'slug' => 'required|min:2|unique:catalog,slug,'.$id,
        ];
    }

Error:

array_map(): Argument #2 should be an array

in ValidationRuleParser.php line 89

if I remove this code "unique:catalog,slug'.$id," then it's working

1 answer

  • answered 2018-07-11 19:21 Joel Hinz

    This line gets the whole model bound to the route:

    $this->route('catalog');
    

    So, since you have the model, you can just get the id from it:

    $this->route('catalog')->id;