How to validate without request in Laravel
Validator::make expects array and not a request object. You can pass any array and implements the rules on it.
Validator::make(['name' => 'Tom'], ['name' => 'required', 'id' => 'required']);
And it will validate the array. So $request object is not necessary.
You can achieve this by create request object like so:
$request = new Request([
'id' => 1,
'body' => 'text'
]);
$this->validate($request, [
'id' => 'required',
'body' => 'required'
]);
and thus you will get all the functionality of the Request class
$request->all()
is array not request object.
This code will work:
$data = [
'id' => 1,
'body' => 'text'
];
$validator = Validator::make($data, [
'id' => 'required',
'body' => 'required',
]);
Should be. Because $request->all()
hold all input data as an array .
$input = [
'title' => 'testTitle',
'body' => 'text'
];
$input is your custom array.
$validator = Validator::make($input, [
'title' => 'required|unique:posts|max:255',
'body' => 'required',
]);