Laravel how to validation in controller

In this tutorial learn how to validation in controller all variables in laravel. laravel validation working using the  validator function.

Defining The Routes
First, let's assume we have the following routes defined in our routes/web.php file:
Route::get('post/create', 'UserController@create');

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

Creating The Controller

Next, let's take a look at a simple controller that handles these routes. We'll leave thstoremethod empty for now:
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class UserController extends Controller
{
    /**
     * Show the form to create a new blog post.
     *
     * @return Response
     */
    public function create()
    {
        return view('post.create');
    }

    /**
     * Store a new blog post.
     *
     * @param  Request  $request
     * @return Response
     */
    public function store(Request $request)
    {
        // Validate and store the blog post...
    }
}
validation define above the class package in controller

use Validator;


public function store(Request $request)
{
//print_r($request->all());
$validator = Validator::make($request->all(), [
            'email' => 'email',
            'vat_number' => 'max:13',
            'password' => 'min:6|required_with:password_confirmation|same:password_confirmation',
            'password_confirmation' => 'min:6'
        ]);
        if ($validator->fails()) {
            return redirect('emaillist/create')
                        ->withErrors($validator)
                        ->withInput();
        }
}


No comments:

Post a Comment

how to call ssh from vs code

 To call SSH from VS Code, you can use the built-in Remote Development extension. This extension allows you to open a remote folder or works...