K
K
Kitaro22019-06-29 19:22:26
Laravel
Kitaro2, 2019-06-29 19:22:26

How to add custom validation to Request Laravel?

Hello, I apologize in advance for a possibly stupid question, I'm just not good at it. The essence of the question is that I want to check for example: the car already has a steering wheel, for example, and you don’t need to immediately assign a new steering wheel to the database, you first need to remove the installed steering wheel, I want to display a message that the steering wheel is already available, through the controller wrote a simple working code (which I also simplified, because in the full version there is a check that the steering wheel is not a consumable :)):

$auto = $request->get('auto_id');
  if ($auto != "" ){
          $dubl=Spare::where([
              ['auto_id' , '=' , $auto]
            ])->first();
      }
      if(!isset($dubl->id)||($dubl->id="")){
        $variable->save();
      }else{
       return response()->json('duble');
      }

Then I thought of doing it through App\Http\Requests, and adding the code there along with other checks, but here I got stuck, how to do it I won’t know, all my attempts get the error "MyRequest does not exist" I remove my attempts to insert MyRequest code starts working fine.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
Kitaro2, 2019-07-03
@Kitaro2

$messsages = array(
      //mesages
    );
    $rules = array(
     //rules
    );
    $v = Validator::make($request->all(), $rules, $messsages);
    $v->sometimes('pole_id','unique:table,pole_id',function ($input) {
      //check
    });
    $v->validate();
    $new = Table::create($v->validated());

A
Alexander Pushkarev, 2019-06-29
@AXP-dev

Read this - https://laravel.com/docs/5.8/validation

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question