I diedump this array and there is a number inside data array
^ array:1 [▼
"data" => array:15 [▼
0 => array:15 [▼
"product_name" => "MOBILELEGEND - 86 Diamond"
"category" => "Games"
"brand" => "MOBILE LEGEND"
"type" => "Umum"
"seller_name" => "PT***"
"price" => 19800
"buyer_sku_code" => "ML86"
"buyer_product_status" => true
"seller_product_status" => true
"unlimited_stock" => true
"stock" => 0
"multi" => true
"start_cut_off" => "23:30"
"end_cut_off" => "0:20"
"desc" => "no pelanggan = gabungan antara user_id dan zone_id"
]
1 => array:15 [▶]
2 => array:15 [▶]
]
]
How can I foreach the value inside the number? the number I mean is 0,1,2
You need to use nested foreach to loop through the multidimensional array you have.
let your array be called $arr for example
// $arr = [ 'data' => [ ['name' => 'lorem'], ... ]];
foreach ($arr['data'] as $item) {
foreach ($item as $key => $value) {
// Do whatever you need here...
}
}
Related
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
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');
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']);
I have a variable $cart which stores the details of product. I want to get a particular field from the cart.
dd($cart) show the following result.
Cart {#437 ▼
+items: array:1 [▼
"airports_334_64" => array:4 [▼
"qty" => 1
"price" => 1600000
"duration" => 0
"item" => array:28 [▼
"id" => 64
"created_at" => "2017-05-29 10:24:22"
"updated_at" => "2017-05-29 10:26:51"
"title" => "Airport ad"
"price" => "120000"
"location" => "Airport T3"
"city" => "Delhi"
"state" => "Delhi"
"rank" => "12"
"landmark" => "abc"
"description" => "<p>new</p>"
"image" => "1496053462.jpg"
"references" => ""
"status" => "Available"
"display_options" => null
"light_option" => null
"airportnumber" => null
"discount" => "1"
"slug" => null
"reference_mail" => "chingkhei91#gmail.com"
"airports_id" => "64"
"area" => "arrival_check_in_hall"
"displayoption" => "backlit_panel"
"dimensions" => "7'10"x3'9""
"optionprice" => "1600000"
"units" => "8"
"ad_code" => ""
"variation_id" => 334
]
]
]
+totalQty: 1
+totalPrice: 1600000
}
I want to get the value of reference_mail i.e Cart->items->item->reference_mail
Use the Collections provided by Laravel:
$items = collect($cart->items);
$reference_mails = $items->map(function($item){
return $item['reference_mail'];
});
This leaves you with an array of all the reference mails. The behaviour of map is described here
If you want to access them then you should iterate over the items like so.
foreach ($cart->items as $items) {
foreach ($items as $item) {
$email = $item['item']['reference_mail'];
}
}
If you need to access it directly based on array index then you could do this.
$email = $cart->items['airports_334_64']['item']['reference_mail'];
If you need the list of reference_mail then you do this in your query rather than going through all this. If you have a collection then you can use pluck method to retrieve the list of emails.
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 = [];