Laravel validate a quantity field in an array of arrays - laravel

selected_products is an array of arrays
^ array:7 [▼
"selected_products" => array:2 [▼
0 => array:2 [▼
"id" => 2
"quantity" => 10
]
1 => array:2 [▼
"id" => 1
"quantity" => 22
]
]
]
I'm trying to validate the quantity of each product if greater than or equal to the quantity in stock I want to get a Out of stock error
Product and Stock is a one to one relationship
Product.php
public function stock () {
return $this->morphOne(Stock::class, 'stockable');
}
This is what I tried so far
StoreActivityRequest.php
'selected_products.*.quantity' => [function ($input, $item) {
return $item > Product::find($input->id)->stock->quantity;
}]
I'm kind lost here

Related

How to search value in collection and merge model in laravel

I have 2 arrays and each array has deeply nested array below is the array
$invoices = $request->user()->invoices();
0 => array:74 [▼
"id" => "in_1MPwwCGBXoaOj5Wmk7lPY9tJ
"lines" => array:5 [▼
"object" => "list"
"data" => array:2 [▼
0 => array:24 [▶]
1 => array:24 [▶]
]
"has_more" => false
"total_count" => 2
"url" => "/v1/invoices/"
]
1 => array:74 [▼
"id" => "in_1MPwwCGBXoaO22vWmk7lPY9tJ
"lines" => array:5 [▼
"object" => "list"
"data" => array:2 [▼
0 => array:24 [▶]
1 => array:24 [▶]
]
"has_more" => false
"total_count" => 2
"url" => "/v1/invoices/"
]
i am trying to get the id from the deep nested array like this
$productnames = $invoices->first()->lines->first()->id;
and then search for the id in the modal like this
$plans = Plan::where('stripe_id', $productnames)->first();
it only display one id, and not all the id from the array,
also, when i merge both the collection it shows result like the below
$invoicescollection = new Collection($invoices);
$planscollection = new Collection($plans);
$products = $invoicescollection->merge($plans)->toArray();
array:10 [▼ // app/Http/Controllers/Billing/BillingController.php:26
0 => array:74 [▶]
1 => array:74 [▶]
"id" => 5
"title" => "All"
"slug" => "all"
"stripe_id" => "price_1MO55aOGBXoaOjvWmuQCJ14b3"
"active" => "0"
"created_at" => null
"updated_at" => null
]
i wanted to show the merge data in each array, not like the above.
thank you for the help.

Laravel validate a field in an array of arrays

selected_products is an array of arrays
^ array:7 [▼
"selected_products" => array:2 [▼
0 => array:2 [▼
"id" => 2
"quantity" => 10
]
1 => array:2 [▼
"id" => 1
"quantity" => 22
]
]
]
I'm trying to validate the quantity of each product if greater than or equal to the quantity in stock I want to get a Out of stock
This is what I tried so far
'selected_products.*.quantity' => [function ($input, $item) {
return $item > 100;
}],

Laravel - manytomany relationship constrain eager load

Many to Many relationship constrain
table:Products
Product_id
Category
Table:Features
Feature_id: 11
Feature_name: remote
Pivot table : FeatureProduct
Feature_id
Product_id
$products = App\Product::with([‘features' => function ($query) {
$query->where(‘id’, 11);
}])->where(‘category_id’, 17)->get();
Still get all products with category = 11, not filter out the feature_id = 11
the :with Query where clause, i don't know what should i put, it is the ID belongs to feature.
This is how a single product output
Product {#1372 ▼
#original: array:13 [▼
"id" => 1
"code" => "PL6881"
"v_description" => "E14 41 bóng + LED pha lê 642 hạt"
"width" => 1200
"length" => null
"height" => 1800
"price" => 47800000
"vendor_id" => 1
"category_id" => 17
"active" => 1
"discount" => 0
"hero" => 0
"promote" => 0
]
#relations: array:1 [▼
"features" => Collection {#5858 ▼
#items: array:3 [▼
0 => Feature {#3149 ▼
#attributes: array:4 [▼
"id" => 3
"code" => "Chau Au"
"v_description" => "Châu Âu"
"att_cate_id" => 1
]
}
1 => Feature {#3340 ▶}
2 => Feature {#3492 ▶}
]
}
]
}
Try the below code,
$products = App\Product::with([‘features'])
->whereHas(‘features', function ($query) {
$query->where(‘id’, 11);
})->where(‘category_id’, 17)->get();
I have edited the answer. please check now
I actually found this useful
$products = Product::with('features')->whereHas('features', function ($query) {
return $query->where('id', 3);
})->where('category_id',17)->get();

How to get the least path in kilometer distancematrix?

I want to get laravel routes between two points and choose the path with less kilometers ...
But the distancematrix returns me with the least amount of time ... what's your offer.
The code below is my code:
$response = \GoogleMaps::load('distancematrix')
->setParam([
'origins' => $origin,
'destinations' => $destination,
'travelMode' => 'driving',
'traffic_model' => 'pessimistic',
'departure_time' => 'now'
])
->get();
$response = json_decode($response, true);
The response that you get contains rows array which then contains elements array. In there you have, distance, duration, duration_in_traffic and so on..
Something like this:
"rows" => array:1 [▼
0 => array:1 [▼
"elements" => array:1 [▼
0 => array:4 [▼
"distance" => array:2 [▼
"text" => "2,527 km"
"value" => 2526659
]
"duration" => array:2 [▼
"text" => "1 day 1 hour"
"value" => 90807
]
"duration_in_traffic" => array:2 [▼
"text" => "1 day 7 hours"
"value" => 111628
]
"status" => "OK"
]
]
]
]
So based on this you can filter/sort the results based on the distance value.

values are not being stored correctly in the array $items

I want to get the quantity of each registration type with the code below:
$registrationTypeDetails = Registration::with('participants:id,registration_type_id,registration_id')->find($regID);
$type_counts = [];
foreach ($registrationTypeDetails->participants as $p) {
$name = $p->registration_type->name;
if (!isset($type_counts[$name])) {
$type_counts[$name] = 0;
}
$type_counts[$name]++;
}
The dd($type_counts), if the conference has 2 registration types available (general and plus) and the user is doing a registration with 2 participants in the registration type "general" and 0 participants in the registration type "plus" shows:
array:2 [▼
"general" => 2
]
Then I need to make a post request to an API where is necessary to send in the request body the quantity of each registration type, in this case there is only 1 registration type "general" and the value for quantity should be "2".
So I have this code below the above code to create the array:
foreach ($registrationTypeDetails->participants as $registrationType) {
$items['invoice']['items'][] = [
'name' => $registrationType->registration_type->name,
'unit_price' => $registrationType->registration_type->price,
'quantity' => $type_counts[$registrationType->registration_type->name],
];
}
$create = $client->request('POST', 'https://...', [
'query' => ['api_key' => '...'], 'json' => $items,
]);
But then the output array is:
array:1 [▼
"invoice" => array:4 [▼
"client" => array:7 [▶]
"items" => array:2 [▼
0 => array:5 [▼
"name" => "general"
"unit_price" => 10
"quantity" => 2
]
1 => array:5 [▼
"name" => "general"
"unit_price" => 10
"quantity" => 2
]
]
]
]
Instead of only 1 item:
array:1 [▼
"invoice" => array:4 [▼
"client" => array:7 [▶]
"items" => array:2 [▼
0 => array:5 [▼
"name" => "general"
"unit_price" => 10
"quantity" => 2
]
]
]
]
$items shows:
array:1 [▼
"invoice" => array:4 [▼
"items" => array:2 [▼
1 => array:5 [▼
"name" => "general"
"unit_price" => 10
"quantity" => 2
]
2 => array:5 [▼
"name" => "plus"
"unit_price" => 0
"quantity" => 2
]
]
]
]
You would need to key the results of the items according to the registration type name:
foreach ($registrationTypeDetails->participants as $registrationType) {
$items['invoice']['items'][$registrationType->registration_type->name] = [
'name' => $registrationType->registration_type->name,
'unit_price' => $registrationType->registration_type->price,
'quantity' => $type_counts[$registrationType->registration_type->name],
];
}
Better is to use the id of the registration_type if there is one:
foreach ($registrationTypeDetails->participants as $registrationType) {
$items['invoice']['items'][$registrationType->registration_type->id] = [
'name' => $registrationType->registration_type->name,
'unit_price' => $registrationType->registration_type->price,
'quantity' => $type_counts[$registrationType->registration_type->name],
];
}
Explanation
As you loop over the participants linked to a registration type multiple participants could have the same registration type. So in your case you need to group the results according to registration type. Just adding to items[] appends to the items array. You want to group the results by registration type.
Fixing the 422 validation error
Your items is a valid array, but because it is translated to json the 1 and 1 would be keyed as an object. To force the key adjustment you can do the following:
// It is hacky but it re-keys the items array numerically.
$items['invoice']['items'] = array_values($list['invoice']['items']);

Resources