Validating Arrays not working in laraval 9 - laravel

Validating Arrays not working in laraval 9.
Request as follows
array:2 [
0 => array:4 [
"nic" => "908110248V"
"employee_id" => "1"
"request_id" => "2"
"schedule_training_id" => "1"
]
1 => array:4 [
"nic" => "962930898v"
"employee_id" => "2"
"request_id" => "1"
"schedule_training_id" => "1"
]
]
validator code snipit as follows
$validator = Validator::make($request->input('data_attributes'), [
'data_attributes.*.nic' => 'required|max:9'
]);

$validator = Validator::make($request->input('data_attributes'), [
'*.nic' => 'required|max:9'
]);

You should check the matrix as a whole and then start working with its elements, this is an example of what you should do:
$validator = Validator::make($request->input('data_attributes'), [
"data_attributes" => "required|array|min:3",
"data_attributes.*" => "required|string|distinct|min:3",
]);

Related

How to set required_if in laravel 7

I have two textboxes color_name[] and color_code[],
How to put required_if? If the color code is not empty, the color name is required.
'color_name' => 'required_if:color_code',
error
Validation rule required_if requires at least 2 parameters.
$validator = Validator::make($request->all(), [
'color_name' => 'required_with:color_code',
]);
if ($validator->fails()) {
return $validator->errors()->first();
}
Use this to solve the problem.
For array please re shape your array into this
array:2 [
0 => array:2 [
"color_name" => "red"
"color_code" => "#77"
]
1 => array:2 [
"color_name" => "blue"
"color_code" => "#88"
]
]
$validator = Validator::make($request->all(), [
'color'=>'array',
'color.*.color_name' => 'required_with:color.*.color_code',
]);

Compare two columns of a collection in laravel?

I want to compare two columns of a collection.I don't want to do this in db level, only in collection level.
I have a collection like this (I returned it in array model for readability)
array:3 [
0 => array:5 [
"total_amount" => 200000.0
"admin_max_amount" => "200000000"
]
1 => array:5 [
"total_amount" => 100000.0
"admin_max_amount" => "200000000"
]
2 => array:5 [
"total_amount" => 100000.0
"admin_max_amount" => "0"
]
]
I want to get first of them where total_amount > admin_max_amount.
Here is a more 'Laravel' way to do it:
$items = collect([
[
"total_amount" => 200000.0,
"admin_max_amount" => "200000000",
],
[
"total_amount" => 100000.0,
"admin_max_amount" => "200000000",
],
[
"total_amount" => 100000.0,
"admin_max_amount" => "0",
],
]);
$result = $items->first(function ($item)
{
return (float)$item['total_amount'] > (float)$item['admin_max_amount'];
});
dump($result);

Laravel 5.6 Validation Array Data

When validating a request data, we are getting the result filtered out by the rules. For example, the following request:
$this->post(route('test'), [
'foo' => 'bar',
'john' => 'doe'
]);
And in my controller, I have the following:
$data = request()->validate([
'foo' => 'required|string'
]);
Then if I perform a dd($data);, I would get the following:
array:1 [
"foo" => "bar"
]
So far so good. However, when working with arrays, for example, the following request:
$this->post(route('test'), [
'foo' => 'bar',
'names' => [
'en' => 'Product',
'fr' => 'Produit',
'john' => 'Doe'
]
]);
And then in my controller, I have the following:
$data = request()->validate([
'foo' => 'required|string',
'names' => 'array',
'names.en' => 'required|string',
'names.fr' => 'required|string'
]);
It would return the following $data:
array:2 [
"foo" => "bar"
"names" => array:3 [
"en" => "Product"
"fr" => "Produit"
"john" => "doe"
]
]
Now my questions are:
Why is 'john' => 'doe' in my names key when I don't have it in my validation rules?
Is there a way to remove unspecified array keys from the data?
Update
I've tested the suggestion of #JonasStaudenmeir, however the result is not what I expected.
Request:
$this->post(route('test'), [
'names' => [
'en' => 'Doe',
'fr' => 'John'
]
]);
Controller:
$rules = [
'names' => 'array',
'names.en' => 'string'
];
dd(request()->only(array_keys($rules));
Expected Output:
array:1 [
"names" => array:2 [
"en" => "Doe"
]
]
Actual Output:
array:1 [
"names" => array:2 [
"fr" => "John"
"en" => "Doe"
]
]
For cases with only nested rules ('names.en') but no parent rule ('names'),
this will be fixed in 5.7: https://github.com/laravel/framework/pull/23708
Until then you can use this workaround:
$data = request()->only(array_keys($rules));
If you do you have a parent rule, this would be much more complicated to achieve.
I would argue that receiving the whole 'names' array in the validated data can be seen as the expected behavior. The idea is that the validator returns the data for each rule. In your case it returns the data for the 'names' => 'array' rule, which is the whole 'names' array.

Laravel validation required_without_all

Call me stupid, but I can't get it :)
I want to set up validator rule, so it will pass only if one of the two fields is present (adgroup or all_adgroups).
Here is my controller:
$this->validate($request,
[
'new_target_cpa_value' => 'required|numeric',
'adgroups' => 'exists:google.ad_groups,id|required_without_all:all_agroups',
'all_agroups' => 'required_without_all:adgroups'
]
);
dd($request->all());
Here is dd:
"_token" => "aHjluUXPuZpEbglmVt4UePhriGvRWDOjk3OgfF88"
"new_target_cpa_value" => "123"
"adgroups" => array:1 [▶]
"all_agroups" => "1"
Try this:
$this->validate($request,
[
'new_target_cpa_value' => 'required|numeric',
'adgroups' => 'exists:google.ad_groups,id',
'all_agroups' => 'required_unless:adgroups,null'
]
);

Laravel multiple records update with Eloquent

I want to Update multiple records to the reference table. Here is the response of the $request.
array:21 [
"_method" => "PATCH"
"_token" => "xXukmVEsMvyeBODIURqFx9Mhk4LviYrV6iLmAuOY"
"account_type" => "0"
"name" => "test"
"model_id" => "2"
"location" => "USA"
"serial_no" => "00055555"
"status" => "Sales"
"capacity_lower" => ""
"weight" => "1212.000"
"category" => "1"
"attribute" => array:3 [
1 => array:1 [
1 => "1"
]
3 => array:2 [
3 => "5"
2 => "2"
]
4 => array:5 [
5 => "11"
6 => "13"
4 => "9"
7 => "23"
8 => "37"
]
]
"crane_manufacture_id" => "1"
"condition" => "Average"
"manufacture_year" => "2020"
"unit_no" => "222222"
"hours" => "100"
"video_url" => ""
"sub_category" => "4"
"description" => "dsfs"
"productImages" => array:7 [
0 => array:3 [
"id" => "27"
"descripton" => "89d9a500-a936-4510-9ca5-5952ee9c0bff.jpg"
"status_id" => "2"
]
1 => array:3 [
"id" => "28"
"descripton" => "89d9a500-a936-4510-9ca5-5952ee9c0bff.jpg"
"status_id" => "2"
]
2 => array:3 [
"id" => "29"
"descripton" => "89d9a500-a936-4510-9ca5-5952ee9c0bff.jpg"
"status_id" => "2"
]
3 => array:3 [
"id" => "30"
"descripton" => "89d9a500-a936-4510-9ca5-5952ee9c0bff.jpg"
"status_id" => "2"
]
4 => array:3 [
"id" => "31"
"descripton" => "89d9a500-a936-4510-9ca5-5952ee9c0bff.jpg"
"status_id" => "2"
]
5 => array:3 [
"id" => "32"
"descripton" => "89d9a500-a936-4510-9ca5-5952ee9c0bff.jpg"
"status_id" => "2"
]
6 => array:3 [
"id" => "33"
"descripton" => "89d9a500-a936-4510-9ca5-5952ee9c0bff.jpg"
"status_id" => "2"
]
]
]
I have two tables, Products and product_images. I have given relation that one product has multiple images. I have apply hasMany relationship in both table.
Now at the time of Update record I am follow the below code for updating product_images records.
DB::enableQueryLog();
$product = Product::find($id);
//$product->save($request->input());
$product->productImages()->update(new ProductImage($request->input('productImages')));
dd(DB::getQueryLog());
But I am facing error with "MassAssignmentException in Model.php line 452:
0"
Check your model both products and product_images has fillable property. refer docs
protected $fillable = ['list of columns to be inserted'];
Check the fillable property protected $fillable = []; or use for all properties from the $request protected $guarded = [];

Resources