Laravel FormRequest Validation For RESTfull Api and Web





API Controller

<?php
namespace App\Http\Controllers\api;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Transaction;
use App\Http\Resources\TransactionResource;
use App\Http\Requests\StoreTransactionRequest;
class TransactionController extends Controller
{
    public function index()
    {
        $model = Transaction::get();
        return new TransactionResource($model);
    }
    
    public function store(StoreTransactionRequest $request)
    {
        $postData = $request->validated();
        $model = Transaction::create($postData);
        return new TransactionResource($model);
    }
}
Web Controller
<?php
namespace App\Http\Controllers;
use App\Models\Transaction;
use App\Http\Requests\StoreTransactionRequest;
use App\Http\Requests\UpdateTransactionRequest;
class TransactionController extends Controller
{
    public function index()
    {
        $model = Transaction::get();
        return view('transaction.list',compact('model'));
    }
    public function create()
    {
        $model = new Transaction();
        return view('transaction.create',compact('model'));
    }
    public function store(StoreTransactionRequest $request)
    {
        $postData = $request->validated();
        $model = Transaction::create($postData);
    }
}
FormRequest:
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Contracts\Validation\Validator;
class StoreTransactionRequest extends FormRequest
{
    public function authorize()
    {
        return true;
    }
    public function rules()
    {
        return [
            'title' => 'required',
            'detail' => 'required',
        ];
    }
    public function failedValidation(Validator $validator)
    {
        // throw new HttpResponseException(response()->json([
        //     'success'   => false,
        //     'message'   => 'Validation errors',
        //     'data'      => $validator->errors()
        // ]));
        // OR
        throw new HttpResponseException(response()->json($validator->errors(), 422));
        // if ($this->wantsJson()) {
        //     throw new HttpResponseException(response()->json($validator->errors(), 422));
        // }
    }
    public function messages() //OPTIONAL
    {
        return [
            'title.required' => 'Title is required',
            'detail.email' => 'Detail is not correct'
        ];
    }
}
Routes:
API:
    Route::get('/transaction/list', [TransactionController::class,'index'])->name('transaction.list');
    Route::post('/transaction/save', [TransactionController::class,'store'])->name('transaction.save');
WEB:
    Route::get('/transaction/list', [TransactionController::class,'index'])->name('transaction.list');
    Route::get('/transaction/create', [TransactionController::class,'create'])->name('transaction.create');
    Route::post('/transaction/save', [TransactionController::class,'store'])->name('transaction.store');

Resource:
https://dev.to/dsazup/respond-to-multiple-content-types-in-laravel-from-the-same-controller-5fem
https://laravel.com/api/5.5/Illuminate/Foundation/Http/FormRequest.html

Share on Google Plus

About Ram Pukar

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment

0 comments:

Post a Comment