Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unique_with on update always says combination exists (2.0.1 ) #31

Closed
eduardogoncalves opened this issue May 9, 2015 · 2 comments
Closed

Comments

@eduardogoncalves
Copy link

In my ProductRequest.php I have set this:

public function rules()
    {
        return [
            'name' => 'required|min:3|unique_with:products,category_id',
            'price' =>'required'
        ];
    }

And in my ProductsController.php I have this:

public function update(Product $product, ProductRequest $request)
    {
        $product->update($request->all());
        return redirect('products');
    }

But when I try to Update a field, like product's price, it gives me the message:

This combination of name, category id already exists.

Could someone help me?

@felixkiss
Copy link
Owner

Define a separate ProductUpdateRequest.php:

public function rules()
{
    return [
        'name' => 'required|min:3,
        'price' =>'required'
    ];
}

It would also be possible to specify one ID that should be ignored, but I think this is not possible with Laravel 5 Request objects.

@eduardogoncalves
Copy link
Author

Hi, I was playing with Request class, printing value of $this and this is what I've got:

public function rules()
    {
        switch($this->method())
        {
            case 'POST':
            {
                return [
                    'name' => 'required|min:3|unique_with:products,category_id',
                    'price' =>'required'
                ];
            }
            case 'PATCH':
            {
                return [
                    'name' => 'required|min:3|unique_with:products,category_id,'.$this->products->id,
                    'price' =>'required'
                ];
            }
        }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants