request()->validate() does not work the second time around n laravel 8

Viewed 376

I have a method to store and validate data from POST request, the first method works fine and validated successfully.

public function saveClientInfo(){
        ClientInfo::create($this->validateClientInfo());
    }
    public function validateClientInfo(){
        return request()->validate([
            'code' => 'required',
            're' => ['required','unique:client_infos'],
            'name' => 'required',
            'contact' => 'required',
            'email' => 'required',
            'property' => 'required',
            'status' => 'required'
        ]);
    }

Here in this second method with the same structure for saving and validating the requests, it doesn't work I'm very intrigue as to why.

public function loanStatus(){
        PaymentInfo::create($this->validatePay());
    }
    public function validatePay(){
        return request()->validate([
            'payment_type' => 'required',
            'terms' => 'required',
            'amount_due' => 'required',
            'from' => 'required',
            'to' => 'required'
        ]);
    }

By the way they're in the same controller so I dont get nauseous trying to figure out what is the difference of both as to where I went wrong.

NOTE:

the error it gives is Route GET is not Supported suggested Method POSTS some sort of like this,

that's why it's confusing enough already I checked the form and routes and I used post so why this error is showing so I figure it must be the request validate part.

Blade File

 <form action="/proceed/loan-status" method="POST">
                @csrf
                <input type="hidden" name="re" value="{{ $re }}">
                <div class="row">
                    <div class="p-2">
                        <input type="text" class="form-control" name="payment-type" placeholder="Payment Type...">
                        @if($errors->has('payment_type'))
                              <p class="alert text-danger">{{ $errors->first('payment_type') }}</p>
                        @endif
                    </div>
                    <div class="p-2">
                        <input type="text" class="form-control" name="terms" placeholder="Terms...">
                    </div>
                    <div class="p-2">
                        <input type="number" min="2" max="" step="any" class="form-control" name="amount" placeholder="Amount Due...">
                    </div>
                    <div class="p-2">
                        <input type="text" class="form-control" name="from" placeholder="From...">
                    </div>
                    <div class="p-2">
                        <input type="text" class="form-control" name="to" placeholder="To...">
                    </div>
                </div>

        </div>
        <div class="card-footer d-flex justify-content-between">
            <button type="submit" class="btn btn-success btn-sm">Save</button>
            <a href="/" class="btn btn-danger btn-sm">Cancel</a>
        </div>
        </form>

web.php

Route::post('/proceed/loan-status', 'ClientInfoController@loanStatus');
4 Answers

If your Laravel version is 8, change the route sintax

Route::post('/proceed/loan-status', ClientInfoController::class,'loanStatus')->name('proceed.loan-status');

and in the blade form change the action to

<form action="{{ route('proceed.loan-status') }}" method="POST">

Also, I suggest you use validation request like Laravel doc, it's a better coded controller and adjusted to patterns. Changing that, you must have something like

public function loanStatus(StorePaymentInfoRequest $request){
  PaymentInfo::create($request->validated());
}

and in the StorePaymentInfoRequest

/**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'payment_type' => 'required',
            'terms' => 'required',
            'amount_due' => 'required',
            'from' => 'required',
            'to' => 'required'
        ];
    }

The error look like you have defined post route in web.php but you are requesting form method is GET or vice versa .So it throwing error

So you need to change form method to post to support that

<form method="POST" >

Or if you still need get request in form method then you need to update routes form post to get

in your Route put your code like this ROUTE::POST('/VARRIBALE NAME', [CONTROLLER NAME::CLASS, 'FUNCTIONNAME']->NAME('VARRIBALE NAME.FUNCTIONNAME);

Related