skip to Main Content

When i make a validation inside the Controller like in the following Laravel example:

Validator::make($request->all(), [ 'credit_card_number' => 'required_if:payment_type,cc' ]);

I can easily access the $request because I’m inside the controller method. However, I’ve noticed that using custom Request classes is considered a best practice in Laravel. I have a question regarding this: Is it possible to access the request variable inside the Request class?

2

Answers


  1. Yes it is possible , and it is simple to access request inside Request class.

    Your Request class :

     public function rules()
    {
        $paymentType1 = $this->request->input('payment_type');
        //OR 
        $paymentType2 = $this->input('payment_type');
    
        return [
            'credit_card_number' => 'required_if:payment_type,cc',
        ];
    }
    
    Login or Signup to reply.
  2. As stated above, it is possible.
    I would recommend reading through the Laravel best practices HERE
    They provided me with lots of helpful tips to stick by.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search