I use the below code for pagination an array so for the first page everything is fine and the response is shown correctly but for the next pages, I have a problem, in the paginated array add a key per each object that I didn't want that so how can I fix that?
public function currentOrders(Request $request)
{
$user = $request->user();
$orders = Order::with('address','foods')->where(['order_status'=>0,'user_id' => $user->id])->get();
$data=[];
if (sizeof($orders)>0) {
foreach ($orders as $order){
$tmp['date'] = Jalalian::forge($order->created_at)->format('%B %d، %Y');
$tmp['time'] = Jalalian::forge($order->created_at)->format('H:i');
$tmp['total'] = $order->sum_price;
$tmp['discount_pay'] = $order->price_off;
$address = Address::withTrashed()->whereId($order->address_id)->first();
$tmp['address'] = optional($address)->address;
$tmp['foods'] = [];
foreach ($order->foods as $food){
$price = 0;
$foodDetail = Article::withTrashed()->find($food->food_id);
$products = OrderItem::with('product')->where(['order_id'=>$food->order_id,'food_id'=>$food->food_id])->get();
$sec_tmp['products'] = [];
foreach ($products as $product){
$third_tmp['product_name'] = $product->product->title;
$third_tmp['count'] = $product->count;
$product_price = ($product->product->price->price_off ? $product->product->price->price_off : $product->product->price->customer_price) * $product->count;
$third_tmp['price'] = $product_price;
$price += $product_price;
array_push($sec_tmp['products'] ,$third_tmp );
}
$sec_tmp['food_name'] = $foodDetail->title;
$sec_tmp['food_count'] = $food->count;
$sec_tmp['food_price'] = $price * $food->count;
array_push($tmp['foods'],$sec_tmp);
}
array_push($data,$tmp);
}
}
$data_collection = collect($data);
$data_paginated = $this->paginate($data_collection);
return response()->json(['orders'=>$data_paginated]);
}
public function paginate($items, $perPage = 2, $page = null, $options = [])
{
$page = $page ?: (Paginator::resolveCurrentPage() ?: 1);
$items = $items instanceof Collection ? $items : Collection::make($items);
return new LengthAwarePaginator($items->forPage($page, $perPage), $items->count(), $perPage, $page, $options);
}
my current result is
{
"orders": {
"current_page": 1,
"data": [
{
"date": "november 19، 2020",
"time": "01:38",
"total": 976400,
"discount_pay": 926400,
"address": "main st",
"foods": [
{
"products": [
{
"product_name": "oil",
"count": 3,
"price": 15000
},
{
"product_name": "egg",
"count": 5,
"price": 362000
},
{
"product_name": "fish",
"count": 1,
"price": 111200
}
],
"food_name": "sosage",
"food_count": 2,
"food_price": 976400
}
]
},
{
"date": "november 19، 2020",
"time": "01:43",
"total": 976400,
"discount_pay": 926400,
"address": "main street",
}
],
"first_page_url": "/?page=1",
"from": 1,
"last_page": 2,
"last_page_url": "/?page=2",
"next_page_url": "/?page=2",
"path": "/",
"per_page": 2,
"prev_page_url": null,
"to": 2,
"total": 4
}
}
but what I have in next pages is :
{
"orders": {
"current_page": 2,
"data": {
"1": {
"date": "november 19، 2020",
"time": "01:43",
"total": 976400,
"discount_pay": 926400,
"address": "main st",
"foods": [
{
"products": [
{
"product_name": "egg",
"count": 3,
"price": 15000
},
{
"product_name": "oil",
"count": 5,
"price": 362000
},
{
"product_name": "fish",
"count": 1,
"price": 111200
}
],
"food_name": "susage",
"food_count": 2,
"food_price": 976400
}
]
}
},
"first_page_url": "/?page=1",
"from": 2,
"last_page": 4,
"last_page_url": "/?page=4",
"next_page_url": "/?page=3",
"path": "/",
"per_page": 1,
"prev_page_url": "/?page=1",
"to": 2,
"total": 4
}
}
In the data object, there is a key like "1": { ... } and I want to be the same as the first page what should I do?
$data was an array but you get an object with key 1. it looks like the page number itself. But You're supposed to have a collection of $temp in $data;
You can check if $data_paginated['data'] is an array before returning return response()->json(['orders'=>$data_paginated]);
I think the error is on $this->paginate($data_collection) where $data_collection is a collection of items from list of $temp.
or could be on collect($data).
I see no error elsewhere.
Related
I have a Laravel collection where I have data for users (their posts, comments and reputation). I would like to transform this collection where each item becomes one of the three properties (posts, comments and reputation).
So the result would be like - for field: 'posts' and user_id: 46 we have 1542 posts. And for field: 'comments' the same user has 2. etc.
I tried to create multiple loops over the collection to get to this result but it is not working as I don't know how may loops I need. Is there a better and more clean way to do this?
Here is my original collection
"data": [
{
"user_id": 46,
"username": "johnive",
"posts": 1542,
"comments": 2,
"reputation": 48.5,
},
{
"user_id": 30,
"username": "zacky13",
"posts": 54,
"comments": 16,
"reputation": 14.3,
},
{
"user_id": 107,
"username": "lil_elf4",
"posts": 564,
"comments": 60,
"reputation": 67.5,
},
],
Here is the result I would like to achieve
"data": [
{
"user_id": 46,
"username": "johnive",
"field": "posts",
"value": 1542
},
{
"user_id": 46,
"username": "johnive",
"field": "comments",
"value": 2
},
{
"user_id": 46,
"username": "johnive",
"field": "reputation",
"value": 48.5
},
{
"user_id": 30,
"username": "zacky13",
"field": "posts",
"value": 54
},
{
"user_id": 30,
"username": "zacky13",
"field": "comments",
"value": 16
},
...
],
I tried using multiple loops seems like a dirty way to do it and I don't know how many loops I would need.
foreach ($collection as $item)
{
$new_item = new stdClass;
$new_item->user_id = $item->user_id;
$new_item->username = $item->username;
$new_item->field = 'posts';
$new_item->value = $item->posts;
}
foreach ($collection as $item)
{
$new_item = new stdClass;
$new_item->user_id = $item->user_id;
$new_item->username = $item->username;
$new_item->field = 'comments';
$new_item->value = $item->comments;
}
foreach ($collection as $item)
{
$new_item = new stdClass;
$new_item->user_id = $item->user_id;
$new_item->username = $item->username;
$new_item->field = 'reputation';
$new_item->value = $item->reputation;
}
You could do it e.g. like this:
$result = collect();
foreach ($collection as $item)
{
foreach(['posts', 'comments', 'reputations'] as $type) {
$new_item = new stdClass;
$new_item->user_id = $item->user_id;
$new_item->username = $item->username;
$new_item->field = $type;
$new_item->value = $item->$type;
$result->push($new_item);
}
}
I have the table "orders" from woocommerce.
I filtered and get sorted the elements by status with GET request.
The results are copied to a JSON file.
So now, I want to find the elements with the same "product_id" and their values and summarize them to display in screen so I can print them.
For example:
"product_id": 45329,
"variation_id": 0,
"quantity": 1
"product_id": 48911,
"variation_id": 0,
"quantity": 1,
"product_id": 45329,
"variation_id": 0,
"quantity": 1
The output that I want to achieve is this:
45329 quantity 2
48911 quantity 1
Thanks!
Decode JSON with json_decode() and make calculations. Next example uses simplified JSON data, which I think matches the format from WooCommerce (I guess this format is from list orders GET request):
<?php
# JSON
$json = '
[
{
"id": 727,
"line_items": [
{
"id": 315,
"name": "Woo Single #1",
"product_id": 93,
"variation_id": 0,
"quantity": 2,
"tax_class": "",
"subtotal": "6.00",
"subtotal_tax": "0.45",
"total": "6.00",
"total_tax": "0.45",
"taxes": [
{
"id": 75,
"total": "0.45",
"subtotal": "0.45"
}
],
"meta_data": [],
"sku": "",
"price": 3
},
{
"id": 316,
"name": "Ship Your Idea – Color: Black, Size: M Test",
"product_id": 22,
"variation_id": 23,
"quantity": 1,
"tax_class": "",
"subtotal": "12.00",
"subtotal_tax": "0.90",
"total": "12.00",
"total_tax": "0.90",
"taxes": [
{
"id": 75,
"total": "0.9",
"subtotal": "0.9"
}
],
"meta_data": [
{
"id": 2095,
"key": "pa_color",
"value": "black"
},
{
"id": 2096,
"key": "size",
"value": "M Test"
}
],
"sku": "Bar3",
"price": 12
}
]
}
]';
$input = json_decode($json, true);
# Sum
$output = array();
foreach ($input as $order) {
foreach ($order["line_items"] as $item) {
$product_id = $item['product_id'];
$quantity = $item['quantity'];
if (!array_key_exists($product_id, $output)) {
$output[$product_id] = 0;
}
$output[$product_id] += $quantity;
}
}
#Output
foreach ($output as $key => $value) {
echo $key.' quantity '.$value.'<br>';
}
?>
Output:
93 quantity 2
22 quantity 1
I have a 3 tables product, category, attributes. In attributes table there is parent_id used for sub-attributes in a same table.Using loop I get a data.
My code :
$productDetails = Product::with('categories')->get();
foreach ($productDetails as $key => $value) {
foreach ($value->categories as $key => $value) {
$attributes = Attribute::where('product_id',$value->product_id)->where('category_id',$value->id)->where('parent_id',Null)->get();
$value->Attributes = $attributes;
foreach ($attributes as $key => $value) {
$subAttributes = Attribute::where('parent_id', $value->id)->get();
$value->subAttributes = $subAttributes;
}
}
}
Output :
{
"productDetails": [
{
"id": 1,
"title": "Small Size Diamond",
"icon": null,
"status": "Active",
"categories": [
{
"id": 1,
"product_id": 1,
"title": "Sieve Size",
"status": "Active",
"sort_order": 1,
"Attributes": [
{
"id": 1,
"product_id": 1,
"category_id": 1,
"parent_id": null,
"title": "- 2.0",
"status": "Active",
"sort_order": 1,
"subAttributes": [
[
{
"id": 9,
"product_id": 1,
"category_id": 1,
"parent_id": 1, // Attributes table ID
"title": "+ 0000 - 000",
"status": "Active",
"sort_order": 1
},
{
"id": 10,
"product_id": 1,
"category_id": 1,
"parent_id": 1, // Attributes table ID
"title": "+ 000 - 00",
"status": "Active",
"sort_order": 2
}
]
]
}
]
}
]
}
]}
The problem is in first product I gt completed response but in other prodcts in loop I do not get subAttributes data. How can i do this?
Simple, please do not use same variable name in all foreach loop,
Your code should like following
$productDetails = Product::with('categories')->get();
foreach ($productDetails as $pro_key => $pro_value) {
foreach ($pro_value->categories as $cat_key => $cat_value) {
$attributes = Attribute::where('product_id',$cat_value->product_id)->where('category_id',$cat_value->id)->where('parent_id',Null)->get();
$cat_value->Attributes = $attributes;
foreach ($attributes as $att_key => $att_value) {
$subAttributes = Attribute::where('parent_id', $att_value->id)->get();
$att_value->subAttributes = $subAttributes;
}
}
}
so i have this kind of class from my user controller
public function index()
{
$table_data = User::with('CU','pus')->select('id','id_cu','id_pus','name','username','gambar','status','created_at')->filterPaginateOrder();
return response()
->json([
'model' => $table_data
]);
}
it is using trait called filterPaginateOrder and the end result that i receive are:
{
"model": {
"current_page": 1,
"data": [
{
"id": 8,
"id_cu": 0,
"id_pus": 1,
"name": "test1",
"username": "test17",
"gambar": "",
"status": 1,
"created_at": "2018-02-27 06:37:10",
"c_u": null,
"pus": {
"id": 1,
"name": "Puskopdit BKCU Kalimantan"
}
},
{
"id": 1,
"id_cu": 0,
"id_pus": 1,
"name": "tony",
"username": "t0n1zz",
"gambar": null,
"status": 1,
"created_at": "2017-01-01 11:11:11",
"c_u": null,
"pus": {
"id": 1,
"name": "Puskopdit BKCU Kalimantan"
}
}
],
"from": 1,
"last_page": 1,
"next_page_url": null,
"path": "https://bkcuvue.dev/api/v1/user",
"per_page": "2",
"prev_page_url": null,
"to": 2,
"total": 2
}
}
so what if i want to add some data into it? inside data array for each array i want to add "role":"master" how do i do that?
i tried $table_data->push('role','master') and $table_data->put('role','master') from reading about collection (those query in laravel return collection right?) but none of those code working...
There are two ways to go about it.
Add it via a foreach
$table_data = User::with('CU','pus')->select('id','id_cu','id_pus','name','username','gambar','status','created_at')->filterPaginateOrder();
foreach($table_data->data as $data) {
$data->role = 'master';
}
Or add it into the SELECT:
$table_data = User::with('CU','pus')->select('id','id_cu','id_pus','name','username','gambar','status','created_at', DB::raw('"master" as role'))->filterPaginateOrder();
Or you can override collection data of paginator object.
$table_data = User::with('CU','pus')->select('id','id_cu','id_pus','name','username','gambar','status','created_at')->filterPaginateOrder();
$table_data->getCollection()->each(function($user) {
$user->role = 'master';
});
return response()->json([
'model' => $table_data
]);
Given the following two arrays, how can they be merged efficiently to result in the third array?
productData
$productData =
[
{
"product_id": 4,
"type": "electronic",
"name": "monitor",
"specs": {
"HDMI": true,
"VGA": false
}
},
{
"product_id": 5,
"type": "electronic",
"name": "HDMI cable",
"specs": {
"length": "3ft"
}
},
{
"product_id": 6,
"type": "kitchen",
"name": "spoon"
}
]
products
$products =
{
"products": 3,
"per_page": 10,
"current_page": 1,
"data": [
{
"id": 4,
"product_type": "electronic",
"product_id": 6
},
{
"id": 6,
"type": "electronic",
"product_id": 5
},
{
"id": 9,
"type": "kitchen",
"product_id": 4
}
]
}
productsFinal ($productData merged into $products - based on matching combo of product_id/product_id and type/product_type)
$productsFinal =
{
"products": 3,
"per_page": 10,
"current_page": 1,
"data": [
{
"id": 4,
"product_type": "electronic",
"product_id": 6,
// How to merge product data and wrap with "data" key
"data": {
"product_id": 6,
"type": "kitchen",
"name": "spoon"
}
},
{
"id": 6,
"type": "electronic",
"product_id": 5,
// How to merge product data and wrap in "data" key
"data": {
"product_id": 5,
"type": "electronic",
"name": "HDMI cable",
"specs": {
"length": "3ft"
}
}
},
{
"id": 9,
"type": "kitchen",
"product_id": 4,
// How to merge product data and wrap in "data" key
"data": {
"product_id": 6,
"type": "kitchen",
"name": "spoon"
}
}
]
}
I tried different things for the outcome in a foreach loop but still cannot get it to render as intended:
foreach($productData as $productDataItem) {
// when $productDataItem.product_id == $product.product_id && $productDataItem.type == $product.product_type
// move the matching $productDataItem object into matching $product object, wrapped in a new "data" key
}
I don't know Laravel too well. However you can join your data objects quite easily:
<?php
$productData = json_decode('[
{
"product_id": 4,
"type": "electronic",
"name": "monitor",
"specs": {
"HDMI": true,
"VGA": false
}
},
{
"product_id": 5,
"type": "electronic",
"name": "HDMI cable",
"specs": {
"length": "3ft"
}
},
{
"product_id": 6,
"type": "kitchen",
"name": "spoon"
}
]');
$products = json_decode('{
"products": 3,
"per_page": 10,
"current_page": 1,
"data": [
{
"id": 4,
"type": "electronic",
"product_id": 6
},
{
"id": 6,
"type": "electronic",
"product_id": 5
},
{
"id": 9,
"type": "kitchen",
"product_id": 4
}
]
}');
// combine both data objects
foreach($products->data As &$p) {
foreach($productData As $d) {
if(property_exists($p, "product_id") && property_exists($d, "product_id") && property_exists($p, "type") && property_exists($d, "type")) {
if($p->product_id==$d->product_id && $p->type==$d->type) {
//$p = (object) array_merge((array) $p, (array) $d);
$p->data = $d; // updated answer
continue;
}
}
}
}
echo("<pre>");
echo json_encode($products, JSON_PRETTY_PRINT);
?>
You can test the code here: http://sandbox.onlinephpfunctions.com/code/98a50c35ee32c30f0d2be1661f7afb5895174cbe
Update: http://sandbox.onlinephpfunctions.com/code/aeebfdcf4f4db5e960260e931982570cfed19e0e
I would suggest to check this package dingo/api. I assume you want to display some kind of JSON response. Take a look at Transformers. You can do something like this :
<?php
namespace App\Http\Transformers;
use App\Http\Controllers\ProductData;
use League\Fractal\TransformerAbstract;
class ProductsDataTransformer extends TransformerAbstract
{
/**
* Turn this item object into a generic array
*
* #return array
*/
public function transform(ProductData $productdata)
{
return [
'id' => $productdata->id,
'product_type' => $productdata->product_type,
'product /*or data*/' => Product::find($productdata->product_id),
];
}
}
This would find the product by it's ID and look like this :
{
"id": 4,
"product_type": "electronic",
"product" {
"product_id": 6,
"type": "kitchen",
"name": "spoon"
},
},
You can then also create a transformer for Product to take care of your specs attribute to do the same thing.