laravel model object changed unexpectedly while doing map operation on it - laravel

This is some wierd situation in which I am there right now like I have one object
$this->remitter;
if I dump this I get this
App\Models\IpayAppBc\Remitter {#1960
#connection: "ipay_app_bc__write"
+table: "remitter"
+timestamps: false
#primaryKey: "id"
+wasRecentlyCreated: false
#escapeWhenCastingToString: false
#attributes: array:15 [
"id" => 15016478
"name" => "ASKHSYA"
"name_last" => ""
"dob" => null
"mobile" => "1234567"
"limit_expiry" => "{"PB":"202212"}"
"address" => "PANCHKULA PANCHKULA HARYANA"
"city" => "PANCHKULA"
"pincode" => "134117"
"state" => "HARYANA"
"two_factor" => 0
"status" => 0
"otp_status" => 2
"paytm_key" => "h00097ea42dc-a6ed-42bc-b2cf-3b6a876f878b"
"ab_kyc" => 0
]
#original: array:15 [
"id" => 15016478
"name" => "ASKHSYA"
"name_last" => ""
"dob" => null
"mobile" => "9946617986"
"limit_expiry" => "{"PB":"202212"}"
"address" => "PANCHKULA PANCHKULA HARYANA"
"city" => "PANCHKULA"
"pincode" => "134117"
"state" => "HARYANA"
"two_factor" => 0
"status" => 0
"otp_status" => 2
"paytm_key" => "h00097ea42dc-a6ed-42bc-b2cf-3b6a876f878b"
"ab_kyc" => 0
]
Now I did this
$remitterDetails = $this->remitter;
$remitterDetails->beneficiaries = $remitterDetails->beneficiaries->take(50)->map(function ($beneficiary) {
return [
'id' => $beneficiary->bene_id,
'name' => $beneficiary->name,
'account' => $beneficiary->account,
'ifsc' => $beneficiary->ifsc,
'bank' => $beneficiary->bank,
'verificationDt' => $beneficiary->last_success_dt,
];
});
Now the confusing part is this mapping changed my $this->remitter too
after dumping $this->remitter again i get this
App\Models\IpayAppBc\Remitter {#1960
#connection: "ipay_app_bc__write"
+table: "remitter"
#attributes: array:16 [
"id" => 15016478
"name" => "ASKHSYA"
"name_last" => ""
"dob" => null
"mobile" => "1234567"
"limit_expiry" => "{"PB":"202212"}"
"address" => "PANCHKULA PANCHKULA HARYANA"
"city" => "PANCHKULA"
"pincode" => "134117"
"state" => "HARYANA"
"two_factor" => 0
"status" => 0
"otp_status" => 2
"paytm_key" => "h00097ea42dc-a6ed-42bc-b2cf-3b6a876f878b"
"ab_kyc" => 0
"beneficiaries" => Illuminate\Support\Collection {#1956
#items: array:2 [
0 => array:6 [
"id" => "898ddaeff5a54bf2f05a3ce00f5cd13a"
"name" => "DUMMY"
"account" => "37893810436"
"ifsc" => "SBIN0000001"
"bank" => "STATE BANK OF INDIA"
"verificationDt" => "2022-12-01 16:58:57"
]
1 => array:6 [
"id" => "c647579b5881ffc39cb2ffb1fc600634"
"name" => "MRAN DW"
"account" => "3789381043"
"ifsc" => "SBIN0000001"
"bank" => "STATE BANK OF INDIA"
"verificationDt" => null
]
]
#escapeWhenCastingToString: false
}
I am just assuming why this happened is there any referencing happening because as per my knowledge this only happens when I use referencing to any variable ?
any information on this please
Thanks

Related

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

Change SKU column value in collection with map

I have an object :
array:100 [▼
0 => array:62 [▼
"id" => 9407
"name" => "Gramophone (El)"
"slug" => "gramophone-el"
"permalink" => "https://wordpress.dev/index.php/produit/gramophone-el/"
"date_created" => "2016-12-23T16:57:13"
"date_modified" => "2016-12-23T23:10:40"
"type" => "variable"
"status" => "publish"
"featured" => false
"catalog_visibility" => "visible"
"description" => "<p>Des magistrats au tribunal écoutent parler un Gramophone en cours d'audience.</p>\n"
"short_description" => ""
"sku" => 1900123Z0
"price" => ""
"regular_price" => ""
"sale_price" => ""
"date_on_sale_from" => ""
"date_on_sale_to" => ""
"price_html" => ""
"on_sale" => false
"purchasable" => false
"total_sales" => 0
"virtual" => false
"downloadable" => false
"downloads" => []
"download_limit" => -1
"download_expiry" => -1
"download_type" => "standard"
"external_url" => ""
"button_text" => ""
"tax_status" => "taxable"
"tax_class" => ""
"manage_stock" => false
"stock_quantity" => null
"in_stock" => false
"backorders" => "no"
"backorders_allowed" => false
"backordered" => false
"sold_individually" => false
"weight" => ""
"dimensions" => array:3 [ …3]
"shipping_required" => true
"shipping_taxable" => true
"shipping_class" => ""
"shipping_class_id" => 0
"reviews_allowed" => true
"average_rating" => "0.00"
"rating_count" => 0
"related_ids" => array:5 [ …5]
"upsell_ids" => []
"cross_sell_ids" => []
"parent_id" => 0
"purchase_note" => ""
"categories" => array:1 [ …1]
"tags" => array:1 [ …1]
"images" => array:1 [ …1]
"attributes" => array:7 [ …7]
"default_attributes" => []
"variations" => []
"grouped_products" => []
"menu_order" => 0
"_links" => array:2 [ …2]
]
$products->map(function($item){
return $item['sku'] = substr($item['sku'], 0, 7);
})->groupBy('sku');
What I need is to keep the first 7 characters of SKU Field, and group my collection by SKU.
Thing is as I do it, sku is never modified.... What is my error????
You can't do it since groupBy on collection works on sql level, it will alter the query to get the result for you.
What you need is
$groupByProducts = $products->groupBy(DB::raw('substr(sku, 0, 7)'));

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

Laravel 5 - Blade casting collection to array and checking its value

I pass my view a Campaign Collection. If I do
{{ dd($campaign->campaignCreatives->campaignCreativesData) }}
I get something like
Collection {#348 ▼
#items: array:2 [▼
0 => CampaignCreativesData {#349 ▼
#attributes: array:7 [▼
"id" => "5"
"name" => "creativeOption"
"label" => "Other"
"value" => "sdfsdfsdfsdfsdf"
"campaignCreativesId" => "5"
"created_at" => "2016-03-01 10:24:38"
"updated_at" => "2016-03-01 10:24:38"
]
}
1 => CampaignCreativesData {#350 ▼
#attributes: array:7 [▼
"id" => "4"
"name" => "creativeOption"
"label" => "checkboxSelection"
"value" => "Animated GIF"
"campaignCreativesId" => "5"
"created_at" => "2016-03-01 10:24:38"
"updated_at" => "2016-03-01 10:24:38"
]
}
]
}
If I cast it to an array
{{ dd($campaign->campaignCreatives->campaignCreativesData->toArray()) }}
I get something like
array:2 [▼
0 => array:7 [▼
"id" => "5"
"name" => "creativeOption"
"label" => "Other"
"value" => "sdfsdfsdfsdfsdf"
"campaignCreativesId" => "5"
"created_at" => "2016-03-01 10:24:38"
"updated_at" => "2016-03-01 10:24:38"
]
1 => array:7 [▼
"id" => "4"
"name" => "creativeOption"
"label" => "checkboxSelection"
"value" => "Animated GIF"
"campaignCreativesId" => "5"
"created_at" => "2016-03-01 10:24:38"
"updated_at" => "2016-03-01 10:24:38"
]
]
Now I have several checkboxes, and if the value is in the array, I need to check the checkbox. At the moment I have something like this
{!! Form::checkbox('creativeOptions[]', 'Animated GIF', in_array('Animated GIF', $campaign->campaignCreatives->campaignCreativesData->toArray()), ['class' => 'styled', 'id' => 'checkbox1']) !!}
As you can see, Animated GIF is in the array, but the checkbox is not checked.
How can I get the checkbox checked based on what is in the array?
Thanks
You can use the method contains in Laravel collection.
{!! Form::checkbox('creativeOptions[]',
'Animated GIF',
$campaign->campaignCreatives->campaignCreativesData->contains('value', 'Animated GIF'),
['class' => 'styled', 'id' => 'checkbox1']) !!}

Resources