HTTP Request and Form Validation

Author: Al-mamun Sarkar Date: 2020-07-11 15:17:55

এই লেসনে লারাভেল এর HTTP Request এবং Form Validation নিয়ে আলোচনা করবো।দেখবো কিভাবে ফর্ম থেকে ডাটা নিতে হয় এবং সেই ডাটা ভেলিডেট করতে হয়। 

 

Source Code:

Routes:

Route::get('users/create', 'UserController@create');

Route::post('users', 'UserController@store');

 

UserController:

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\User;

class UserController extends Controller
{
	
	public function create()
    {
        return view('user.register');
    }

    public function store( Request $request )
    {
        $request->validate([
            'name' => 'required|string|min:3',
            'email' => 'required|email',
            'password' => 'required|numeric',
        ]);

        // return $data;
        // return $request->input('email');
        return $request->all();
    }

}

 

UserRequest:

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class UserRequest 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|string|min:3',
            'email' => 'required|email|unique:users',
            'password' => 'required|numeric',
        ];
    }
}

 

UserController:

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests\UserRequest;
use App\User;

class UserController extends Controller
{
	
	public function create()
    {
        return view('user.register');
    }

    public function store( UserRequest $request )
    {
        return $request->all();
    }

}