J
J
jazzus2018-12-20 10:43:08
Laravel
jazzus, 2018-12-20 10:43:08

How to check conditions in Laravel Request files?

I use my files for validation in Laravel. Those. validation is not in controllers. In controllers, I can create conditions for validation. It does not work in request files (which are created by the php artisan make:request command )
Sample file:

public function rules()
    {
        return [,
          'name' => 'required|string|max:50',
        ];
    }

    public function messages()
    {
        return [
          'name.required' => 'Имя обязательно для заполнения',
          'name.max' => 'Максимальная длина имени 50 символов',
        ];
    }

I can't attach a condition to this file. Like, if the parameter in the request is such and such, then do a check:
if ($request->input('type_name')==1){
    'name' => 'required|string|max:50',
}

Most likely the input request will not work here. Some other way is needed than in controllers. How to use logic in these files? I would be grateful for an example.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Konstantin B., 2018-12-20
@jazzus

Of course $request won't work here, why? ;))
Yes, because if you turn on your head and first study OOP, and then take on the code, then it’s not difficult to guess that at the moment we are just the same in this very $request
And therefore we can get it through $
this ? Think it's magic? But nooo - this is knowledge and a dock))
What do you think, in order to get the value of type_name now, what should I write?
Correct, $this->input('type_name')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question