Export multidimensional arrays to CSV and JSON using maatwebsite - laravel

I have a question about exporting data.
I am using maatwebsite with laravel 5.4. I have an array of arrays like this:
array:8 [▼
"company_id" => "2"
"company_name" => "Test Company2"
"survey_id" => "1"
"survey_name" => "ELMAO"
"customer_id" => "5"
"customer_first_name" => "Lol"
"customer_last_name" => "Kek"
"questions" => array:2 [▼
0 => array:4 [▼
"question_name" => "1"
"question_title" => ""
"response_text" => "2"
"screenshot_url" => "iVBORw0KGgoAAAANSUhEUgAAAeAAAAHvCAYAAACMggxAAAAgAElEQVR4XuydCZwdVZX/f/W2XpLOvq9kYwkh7BACjKCCrAEMmywqiqij4gAqOjoqo6OOOgoDKjgqyoCIiIp/GEH2fcdAyB5IyL4nvfdb6//5nXqv ▶"
]
1 => array:4 [▼
"question_name" => "7"
"question_title" => "Keek!"
"response_text" => "2"
"screenshot_url" => "iVBORw0KGgoAAAANSUhEUgAAAeAAAAHvCAYAAACMggxAAAAgAElEQVR4XuydB7hcZbX+3z3tlPRGeg+EQAhBSkKAK6ggUkIJTYqKIupV8Qooer1XRb3qVa/CRRFsKBdERFT8g4D03iEQ0kggIb2enH6mz/95156J ▶"
]
]
]
How can I export this data to a CSV file and as a JSON string also?

When retrieving data from the eloquent model you have build in functions to set it to JSON. https://laravel.com/docs/5.5/eloquent-serialization#serializing-to-arrays
https://stackoverflow.com/a/9573700/5871725

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: Receive data from ChargeBee

I managed to create a Customer in ChargeBee but I don't know how to access each of its values.
To receive the information I use the following code, it works perfect for me ...
ChargeBee_Environment::configure("mysite-test","test_afhjsfslfsfslk438493sshgeh");
$result = ChargeBee_Customer::retrieve("16HHGDGJslfksds");
$customer = $result->customer();
$card = $result->card();
By doing a dd($result) I get the following:
ChargeBee_Result {#356 ▼
-_response: array:1 [▼
"customer" => array:22 [▼
"id" => "16HHGDGJslfksds"
"first_name" => "User"
"last_name" => "Test"
"email" => "user#mail.com"
"phone" => "555667788"
"auto_collection" => "on"
"net_term_days" => 0
"allow_direct_debit" => false
"created_at" => 1603420880
"taxability" => "taxable"
"updated_at" => 1603420880
"pii_cleared" => "active"
"resource_version" => 1603420880216
"deleted" => false
"object" => "customer"
"billing_address" => array:6 [▶]
"card_status" => "no_card"
"promotional_credits" => 0
"refundable_credits" => 0
"excess_payments" => 0
"unbilled_charges" => 0
"preferred_currency_code" => "USD"
]
]
-_responseObj: array:1 [▼
"customer" => ChargeBee_Customer {#358 ▶}
]
}
How can I get for example the id of this Customer??? Which in this case is 16HHGDGJslfksds
You can use this line:
$customerId = $result->customer()->id

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 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 = [];

How to check if multidimensional array is empty or not in laravel

I'm trying to check if there is an empty array in the nested arrays.
This is what I get from my form.
array:15 [▼
"_token" => "h4aR4xJlWhZveRKbAgHzgzHWSKSqyhVKb7OHAgWH"
"name" => "Test office"
"is_department" => "0"
"hours" => "1-3"
"description" => "Description"
"content" => "<p>Content</p>"
"street" => "123 Street"
"city" => "Foomania"
"state" => "Sweet state"
"postal" => "98234"
"phone" => "5748293212"
"fax" => "2123131233"
"email" => "test#domain.tld"
"additional-page" => ""
"office_fees" => array:4 [▼
0 => array:2 [▼
"description" => ""
"fee" => ""
]
1 => array:2 [▼
"description" => ""
"fee" => ""
]
2 => array:2 [▼
"description" => ""
"fee" => ""
]
3 => array:2 [▼
"description" => ""
"fee" => ""
]
]
]
How can I check if there is empty array in office_fees ?
Just to be clear, office_fees will always return at least one array. What I'm trying to is to be able to determine whether the office_fees need to be saved into another model.
Not sure what You're looking for but:
empty($data['office_fees'])
checks if an array isset and not empty. If You want to check an empty array try this:
if (is_array($data['office_fees']) && !empty($data['office_fees']))
In laravel 5.2 you can validate arrays
$validator = Validator::make($request->all(), [
'office_fees.*.description' => 'required',
]);
Source: Laravel documentation

Resources