Athsal KJ
Athsal KJ

Reputation: 29

How to add validation for preventing duplicate level points in laravel query?

Here is a table named stages and below are the fields

[1]: https://i.sstatic.net/JAnzA.png

I don't want to allow to add the same from and to points example, if points from 1 to 10 is already added means not allow them to add the same range, another one condition is don't allow to add in between points example in-between 1to 10 like 5 to 7 are also not allowed.

Tried laravel query

$isexist = Stage::whereBetween('from', [$request->from, $request->to]) ->orWhereBetweenColumn('to','from', [$request->from, $request->to])->exists();

but it not satisfying all conditions.

Any one please help, thanks in advance.

Upvotes: 0

Views: 27

Answers (1)

Ashutosh Jha
Ashutosh Jha

Reputation: 154

you can validate the data like this

  $validator = Validator::make($inputData, [
        'label' => 'required|unique:tableName,label'
    ]);
    if($validator->fails()){
        //Throw validation errors
    }

Or you can use

   Model::query()->updateOrCreate(['label' => $request->label], [
        'from' => $request->from,
        'to' => $request->to
    ]);

Read more on unique validation reference: https://laravel.com/docs/9.x/validation#quick-writing-the-validation-logic

Upvotes: 1

Related Questions