/** * Artisan Command */ php artisan make:request RequestName /** * Controller Code */ // inject filepath to custom request use App\Http\Requests\RequestName; // inject custom request as $request // $request knows about validation and will automatically validate public function store(RequestName $request) { Category::create([ 'name' => $request->name ]); session()->flash('success', 'Category Successfully Created'); return redirect(route('categories.index')); } /** * Request File */ class CreateCategoryRequest 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() { // this is where validation rules are set return [ 'name' => 'required|unique:categories' ]; } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter