B
B
BonBon Slick2017-05-02 22:12:10
Validation
BonBon Slick, 2017-05-02 22:12:10

Laravel 5.4 update unique field?

I tried different things, nothing comes out, it returns an error that the field is already in use:

'key' => 'unique:pgsql.public.posts,key', Rule::unique('posts')->ignore($this->id),
//или
'key' => 'unique:pgsql.public.posts,key,',$this->id,
// или
'key' => 'unique:pgsql.public.posts,key,',$id,
$this->get('id')
$this->segment(2)
$this->key

$this->id checked, there is the id of the current record in the database that we are updating. I took an example from the docks
. I also tried:
'key' => [
                'required',
                Rule::exists('pgsql.public.posts')->where(function ($query) {
                    $query->where('id', Post::where('key', $this->key)->first()->id);
                }),
            ],
// и 
'key' => [
                'required',
                               Rule::unique('pgsql.public.posts,key')->where('key', '<>', RollGame::where('key', $this->key)->first()->id),
            ],
// я заменял id на key, это не помогает, только ручная валидация. 
// и еще вариантов 100 пробовал разных.

So how do you skip it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
Flasher, 2017-05-02
@BonBonSlick

Your validation is kind of strange, it should be something like this:

class UserRequest extends Request
{

    public function authorize()
    {
        return true;
    }


    public function rules()
    {
        return [
            'user_cover_address' => 'required|image|mimes:jpeg,jpg,png,gif',
            'user_firstname' => 'required|min:3|max:32',
            'user_lastname' => 'required|min:3|max:32',
            'user_password' => 'required|min:3|max:32',
        ];
    }
}

And in the controller like this:
public function store(Request $request, SignupRequest $rules)
    {
        $this->validate($request, $rules->rules());

        $user = new User($request->all());
        $user->save();
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question