使用Laravel 5.3请求验证

I am trying to validate my api call using laravel built-in request method. I have used --resource to get make it REST.

OneTimePasswordController

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use App\Models\OneTimePassword as Model;
use App\Http\Requests\OneTimePasswordReq;


class OneTimePasswordController extends Controller
{
    /**
    * Display a listing of the resource.
    *
    * @return \Illuminate\Http\Response
    */
    public function index(UserController $user)
    {
        //
        $otps = Model::get();

        return response()->json($otps);
    }

    /**
    * Show the form for creating a new resource.
    *
    * @return \Illuminate\Http\Response
    */
    public function create()
    {
        //
    }

    /**
    * Store a newly created resource in storage.
    *
    * @param  \Illuminate\Http\Request  $request
    * @return \Illuminate\Http\Response
    */
    public function store(OneTimePasswordReq $request)
    {

        $insert = Model::create($request->all());
        return response()->json($insert);
    }

    /**
    * Display the specified resource.
    *
    * @param  int  $id
    * @return \Illuminate\Http\Response
    */
    public function show($id)
    {
        //
    }

    /**
    * Show the form for editing the specified resource.
    *
    * @param  int  $id
    * @return \Illuminate\Http\Response
    */
    public function edit($id)
    {
        //
    }

    /**
    * Update the specified resource in storage.
    *
    * @param  \Illuminate\Http\Request  $request
    * @param  int  $id
    * @return \Illuminate\Http\Response
    */
    public function update(Request $request, $id)
    {
        //
    }

    /**
    * Remove the specified resource from storage.
    *
    * @param  int  $id
    * @return \Illuminate\Http\Response
    */
    public function destroy($id)
    {
        //
        $delete = Model::destroy($id);
        return response()->json($delete);
    }
}

OneTimePasswordReq

    <?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Contracts\Validation\Validator;

class OneTimePasswordReq 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 [
        'mobile' => 'required',
        'code' => 'required',
        ];
    }

    protected function formatErrors(Validator $validator)
    {
        return $validator->errors()->all();
    }

    /**
     * Set custom messages for validator errors.
     *
     * @return array
     */
    public function messages()
    {
        return [
            'mobile.required'=>"Mobile field is required"
        ];
    }
}

If i pass my params, with values, it gets inserted.

Now if i pass a post request with mobile field deleted, i expect a validation errors. But the call is made to index method which fetches all data from the url.

My understanding is the request is rejected because the params is missing and the question is why its changing to index method and how do i get the errors?

Note : I am aware about $validator->fails() concept, which i don't want to put into my controller as laravel offers this.

As i tested it with postman, it was redirecting to the same route and picking as get.

If called with javascript code, the errors are displayed.

To check with postman, you need add in headers

X-Requested-With: XMLHttpRequest

Thanks to all supporters.