laravel custom request code example
Example 1: laravel validate integer between
'item' => 'required|integer|between:1,10',
Example 2: laravel validation
/**
* Store a new blog post.
*
* @param Request $request
* @return Response
*/
public function store(Request $request)
{
$validatedData = $request->validate([
'title' => 'required|unique:posts|max:255',
'body' => 'required',
]);
// The blog post is valid...
}
Example 3: laravel validation types
# = foo,bar,...
# = array field
# = amount of characters
# accepted # active_url
# after: # after_or_equal:
# alpha # alpha_dash
# alpha_num # array
# bail # before:
# before_or_equal: # between:min,max
# boolean # confirmed
# date # date_equals:
# date_format: # different:
# digits: # digits_between:min,max
# dimensions: # distinct
# email # ends_with:
# exclude_if:, # exclude_unless:,
# exists:, # file
# filled # gt:
# gte: # image
# in: # in_array:
# integer # ip
# ipv4 # ipv6
# json # lt:
# lte: # max:
# mimetypes:video/avi,... # mimes:jpeg,bmp,png
# min: # not_in:
# not_regex: # nullable
# numeric # password:
# present # regex:
# required # required_if:,
# required_unless:, # required_with:
# required_with_all: # required_without:
# required_without_all: # same:
# size: # starts_with:
# string # timezone
# unique:, # url
# uuid
Example 4: rule::exists with custom message laravel
$messsages = array(
'email.required'=>'You cant leave Email field empty',
'name.required'=>'You cant leave name field empty',
'name.min'=>'The field has to be :min chars long',
);
$rules = array(
'email'=>'required|unique:content',
'name'=>'required|min:3',
);
$validator = Validator::make(Input::all(), $rules,$messsages);
Example 5: laravel add request
$request->all() + ['index' => 'value'];
Example 6: laravel validation
use Illuminate\Validation\Rule;
Validator::make($data, [
'email' => [
'required',
Rule::unique('users')->ignore($user->id),
],
]);