Laravel - manytomany relationship constrain eager load - laravel

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();

Related

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 validate a quantity 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 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

How to get getClientOriginalName() from Image of array on Laravel?

I Have An Array from request Like This.... I have order_details table, and i want to update column category, remark and foto data where have orderid and skuid that i get from request,...
array:7 [▼
"orderid" => "191121120129KUR"
"_token" => "1ov7aCP8JzzokWNfMpwntTH0Dv084HqwIqMUfLd1"
"orderdet" => array:5 [▼
0 => array:4 [▼
"skuid" => "10006"
"category" => array:2 [▼
0 => "1"
1 => "2"
]
"remark" => "remarks 1"
"foto" => array:2 [▼
0 => Illuminate\Http\UploadedFile {#348 ▶}
1 => Illuminate\Http\UploadedFile {#349 ▶}
]
]
1 => array:4 [▼
"skuid" => "10007"
"category" => array:1 [▼
0 => "1"
]
"remark" => "remarks 2"
"foto" => array:4 [▼
0 => Illuminate\Http\UploadedFile {#350 ▶}
1 => Illuminate\Http\UploadedFile {#351 ▶}
2 => Illuminate\Http\UploadedFile {#352 ▶}
3 => Illuminate\Http\UploadedFile {#353 ▶}
]
]
2 => array:2 [▼
"skuid" => "10008"
"remark" => null
]
3 => array:2 [▼
"skuid" => "10138"
"remark" => null
]
4 => array:2 [▼
"skuid" => "10078"
"remark" => null
]
]
"actioncomplaint" => "1"
"salahsiapa" => "3"
"remarkcust" => null
"remarkfresh" => "Dani"
]
I want to store name of image using getClientOriginalName(),...
How to get that name of images , this is my controller
foreach($request->orderdet as $row)
{
$data = OrderDetail::where('data_order_id', $request->orderid)->where('skuid', $row['skuid'])->first();
if(isset($row['category'])) {
$data->remarkscomplaint = $row['remark'];
$data->categorycomplaint = implode(',', $row['category']);
$data->fotocomplaint = $row['foto'];
$data->save();
}
}
Try something like:
$fotos = $request->only(['foto']);
$fotoNames = []
foreach($fotos as $foto) {
$fotoNames[] = $foto->getClientOriginalName();
}
$implodedFotos = implode(',' $fotoNames);
then
foreach($request->orderdet as $row) {
$data = OrderDetail::where('data_order_id', $request->orderid)->where('skuid', $row['skuid'])->first();
if(isset($row['category'])) {
dd($row['foto']); //Array of Image
$data->remarkscomplaint = $row['remark'];
$data->categorycomplaint = implode(',', $row['category']);
$data->fotocomplaint = $implodedFotos; // <- set your attribute with the variable we create with the names
$data->save();
}
}
I guess you could also do something like
$fotos = collect($request->orderset['foto'])
->map(function ($foto) {
return $foto->getClientOriginalName();
})
->implode(',');
$fotos->all(); // access the result
and then in order to save your images this will help you.
$foto->storeAs('path', 'filename');

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']);

laravel result array of array

I have this eloquent query
$data= \App\Logs::select('id', 'created_at')
->get()
->groupBy(function($val) {
return Carbon::parse($val->created_at)->format('d-M-');
})->toArray();
and this retun something like that
array:2 [▼
"06-Jul-" => array:1 [▼
0 => array:2 [▼
"id" => 1
"created_at" => "2017-07-06 13:21:15"
]
]
"07-Jul-" => array:3 [▼
0 => array:2 [▼
"id" => 2
"created_at" => "2017-07-07 13:43:23"
]
1 => array:2 [▼
"id" => 3
"created_at" => "2017-07-07 14:18:36"
]
2 => array:2 [▼
"id" => 4
"created_at" => "2017-07-07 14:18:41"
]
]
]
and i want return just the count
"06-Jul-" => "1"
"07-Jul-" => "3"
how i can do that?
You can use/chain map/transform after your groupBy for example:
$data = \App\Logs::select('id', 'created_at')
->get()
->groupBy(function($val) {
return Carbon::parse($val->created_at)->format('d-M-');
})
->transform(function ($collection, $key) {
return $collection->count();
});

Resources