0

I am trying to store data using store method and using VendorStoreRequest class for validation. However, I am getting : 405 Method Not Allowed error. When I change to normal Illuminate Request it works.

giving error

use App\Http\Requests\VendorAccount\VendorStoreRequest;
...
public function store(VendorStoreRequest $request)
{
    return response()->json($request, 200);
}

working fine

use Illuminate\Http\Request;
...
public function store(Request $request)
{
    return response()->json($request, 200);
}

I am using resource api route.

Route::prefix('vendor')->group(function () {
    Route::apiResource('/', VendorController::class)->parameters(['' => 'vendor']);
});

VendorStoreRequest.php

<?php

namespace App\Http\Requests\VendorAccount;

use Illuminate\Foundation\Http\FormRequest;

class VendorStoreRequest extends FormRequest
{
    /**
     * 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 [
            'name' => 'required|min:5',
            'email' => 'required|unique:vendors|email',
            'phone' => 'required|digits:11|numeric|unique:vendors',
            'business_phone' => 'required|digits:11|numeric|unique:vendors',
            'password' => 'required|min:6',
            'business_name' => 'required|unique:vendors',
            'trade_licence_no' => 'required|regex:/^[0-9]+$/|max:20|unique:vendors',
            'bank_account_no' => 'required|regex:/^[0-9]+$/|max:20|unique:vendors',
            'nid' => 'required|digits_between:10,13|unique:vendors',
            'image' => 'required|image|mimes:jpg,png,jpeg'
        ];
    }

    public function messages()
    {
        return [
            'name.min' => 'Name should be at least 5 characters',
            'phone.digits' => 'Phone number is not valid',
            'phone.unique' => 'Phone number has already been taken',
            'business_phone.digits' => 'Business phone number is not valid',
            'business_phone.unique' => 'Business phone number has already been taken',
            'password.min' => 'Password should be at least 6 characters',
            'business_name.unique' => 'Business name has already been taken',
            'trade_licence_no.regex' => 'Trade licence number is not valid',
            'trade_licence_no.unique' => 'Trade licence number is duplicate',
            'bank_account_no.regex' => 'Bank account number is not valid',
            'bank_account_no.unique' => 'Bank account number is duplicate',
            'nid.digits_between' => 'NID number is not valid',
            'nid.unique' => 'NID number is duplicate',
            'nid.digits_between' => 'NID number is not valid',
        ];
    }
}
3
  • Adding a curl will allow people to answer your question. Right now, we don't know what you're calling. Commented Nov 13, 2023 at 20:46
  • 1
    Have you tried removing some of the rules and messages to try and limit what is causing the error? Commented Nov 14, 2023 at 0:43
  • yes, after removing and testing i found some mismatch and that's causing the issue. However i provide wrong input, instead of catching the issue and sending the message as ouput it again giving 405 Method Not Allowed error in Laravel 10. Commented Nov 14, 2023 at 14:21

1 Answer 1

0

Found the solution here:

MethodNotAllowedHttpException using Form Request Validation on Laravel 5.5

accept: application/json and Content-Type: application/json

that way laravel knows that its an api request and will use routes defined on api.php and not web.php routes.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.