How to make multiple validations Requests in Laravel?

Asked

Viewed 35 times

0

Laravel allows you to create Requests to validate fields coming from the front end, but I wanted to make several Requests to validate a giant Request. Is it possible? It’s good practice?

I made Traits so they could register each one.

These are the form fields: As you can see they are divided into array

inserir a descrição da imagem aqui

This is the method in the controller you are receiving:

inserir a descrição da imagem aqui

public function store(DealerRequest $request)
{
    try{

        DB::beginTransaction();
            dd($request->all());
            $dealer = $this->dealer->create($request['dealer']);

            $company = $this->createCompany($dealer, $request);

            $this->createPerson($company, $request);

            $this->createLocation($company, $request);

            $this->createContact($company, $request);

            $this->createContract($company, $request);

        DB::commit();

        event(new Welcome($dealer));
        return redirect()->route('representative.dealers.index');
    }catch (\Exception $e){
        dd($e->getMessage());
        DB::rollBack();
        return redirect()->back()->withInput()->with('error', 'Falha no servidor');
    }
}

This method makes calls to other methods that perform Inserts in the database. My idea was that in each method have a request validating their respective fields.

Example:

inserir a descrição da imagem aqui

public function createCompany(Dealer $dealer, CompanyRequest $request){
    try {
        return $dealer->company()->create($request->input());
    }catch (\Exception $e){
        return $e->getMessage();
    }
}

This is a method that instantiates a request, and how it has others instantiating their respective requests. but when executing I have the following error:

inserir a descrição da imagem aqui

I would like a better solution, or how to solve this problem.

Thank you!!

  • Replace the images of the query with text and format the code.

  • What do you mean, sorry, you want me to show you all the code?

  • No XD, just convert the images to text and put with formatting deste modo por exemplo

  • I think ... (I can’t test) but, the traits are repeating the same name $request, try to put another name like $requestCompany and so on!

  • OK, I’ll try. Thank you!

  • That worked, to help better understand, see that when the data of the request enters the method "store()" it is already instantiated by a request, then when it passes the same data to the method createCompany()the array has already become an object and Laravel does not allow this object to make a new instance.

  • 1

    I understand now what you’ve done, what you’ve done doesn’t work!! has to assemble another strategy, in the case in question in the Parent method of all call all request for validation and pass the same corresponding to your internal methods, another matter is performance and maintenance, not if this will be legal.

Show 2 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.