Laravel collection union during each not working - laravel

I have a probleme with union() that not insert my new collection
During my each I have the $mean collection with witch I search the slices() I test and have some $_availabaleSlices to insert into my $slices (id: 4 then id:7)
But after the whole code passed I only have the first $_availabaleSlices inside the $slices.
What I have made wrong ?
The code
$slices = new Collection();
Means::whereIn('id', $meansId)
->get()
->each(function ($mean) use (&$slices, $weight) {
$_availabaleSlices = $mean->slices()
->betweenWeight($weight)
->get();
if (0 < $_availabaleSlices->count()) {
dump('NEEDED',$_availabaleSlices->toArray());
dump('BEFORE',$slices->toArray());
$slices = $slices->union($_availabaleSlices);
dump('AFTER',$slices->toArray());
}
});
dd('RESULTS', $slices->toArray());
The Results
"NEEDED"
array:1 [
0 => array:5 [
"id" => 4
"deliveries_mean_id" => 3
"weight_min" => 250.0
"weight_max" => 600.0
"deleted_at" => null
]
]
"BEFORE"
[]
"AFTER"
array:1 [
0 => array:5 [
"id" => 4
"deliveries_mean_id" => 3
"weight_min" => 250.0
"weight_max" => 600.0
"deleted_at" => null
]
]
"NEEDED"
array:1 [
0 => array:5 [
"id" => 7
"deliveries_mean_id" => 4
"weight_min" => 150.0
"weight_max" => 700.0
"deleted_at" => null
]
]
"BEFORE"
array:1 [
0 => array:5 [
"id" => 4
"deliveries_mean_id" => 3
"weight_min" => 250.0
"weight_max" => 600.0
"deleted_at" => null
]
]
"AFTER"
array:1 [
0 => array:5 [
"id" => 4
"deliveries_mean_id" => 3
"weight_min" => 250.0
"weight_max" => 600.0
"deleted_at" => null
]
]
"RESULTS"
array:1 [
0 => array:5 [
"id" => 4
"deliveries_mean_id" => 3
"weight_min" => 250.0
"weight_max" => 600.0
"deleted_at" => null
]
]

Found the solution. I needed to use ->merge() and not ->union().

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 model object changed unexpectedly while doing map operation on it

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

How to combine two collections by matching keys and their values

So, I've been bashing my head against this for a while and I cant get it to just do what I need it to.
I want to be able to store a set of valuse in our database as Json, but no matter what I cant get Laravel or format the object quite right first.
I've got three queries, One gets users usernames and their First name, as an object. Another two use that object to get a count of each users sales, and quotes, from our databsae with count().
Now all I need is the objects to be combined.
array:1 [
"" => array:3 [
0 => array:7 [
0 => array:2 [
"CallCentreID" => "sdickerson"
"FirstName" => "Sarah"
]
1 => array:2 [
"CallCentreID" => "wjones"
"FirstName" => "Wendy"
]
2 => array:2 [
"CallCentreID" => "aknox"
"FirstName" => "Alex"
]
3 => array:2 [
"CallCentreID" => "mking"
"FirstName" => "Melissa"
]
4 => array:2 [
"CallCentreID" => "nrowecc"
"FirstName" => "Neil"
]
5 => array:2 [
"CallCentreID" => "ejones"
"FirstName" => "Emma"
]
6 => array:2 [
"CallCentreID" => "spurnell2"
"FirstName" => "Simon"
]
]
1 => array:5 [
0 => array:2 [
"CallCentreID" => "aknox"
"Sales" => 1169
]
1 => array:2 [
"CallCentreID" => "ejones"
"Sales" => 401
]
2 => array:2 [
"CallCentreID" => "mking"
"Sales" => 767
]
3 => array:2 [
"CallCentreID" => "sdickerson"
"Sales" => 1067
]
4 => array:2 [
"CallCentreID" => "wjones"
"Sales" => 716
]
]
2 => array:6 [
0 => array:2 [
"CallCentreID" => "aknox"
"Quotes" => 3587
]
1 => array:2 [
"CallCentreID" => "ejones"
"Quotes" => 400
]
2 => array:2 [
"CallCentreID" => "mking"
"Quotes" => 6975
]
3 => array:2 [
"CallCentreID" => "nrowecc"
"Quotes" => 3
]
4 => array:2 [
"CallCentreID" => "sdickerson"
"Quotes" => 2686
]
5 => array:2 [
"CallCentreID" => "wjones"
"Quotes" => 2734
]
]
]
]
The above is as close as I can get with the code I have which is;
$Users = User::where('Accesslevel', 'laravelcallcentre')->select('EmailAddress as CallCentreID', 'FirstName')->get();
$Sales = Order::groupBy('CallCentreID')
// ->whereDate('OrderDate', '=', date('2018-06-21'))
->whereIn('CallCentreID', $Users->pluck('CallCentreID'))
->where('Status', 'BOOKING')
->selectRaw('CallCentreID, count( * ) AS Sales')
->get();
$Quotes = Order::groupBy('CallCentreID')
// ->whereDate('OrderDate', '=', date('2018-06-21'))
->whereIn('CallCentreID', $Users->pluck('CallCentreID'))
->where('Status', 'QUOTE')
->selectRaw('CallCentreID, count( * ) AS Quotes')
->get();
$collection = collect([$Users, $Sales, $Quotes]);
$CCData = $collection->groupBy('CallCentreID');
Anyone got any better Ideas? I feel like im doing this completley wrong. I had this written out as a foreach loop but that ended up being four queries for every user, whihc stacked up fast.
$sales = $sales->keyBy('CallCentreID');
$quotes = $quotes->keyBy('CallCentreID');
$users = $users->map(function($user) use($sales,$quotes) {
$user->sales = isset($sales[$user['CallCentreID']]['sales']) ? $sales[$user['CallCentreID']]['sales'] : 0;
$user->quotes = isset($quotes[$user['CallCentreID']]['quotes']) ? $quotes[$user['CallCentreID']]['quotes'] : 0;
return $user;
});

How to use whereIn() when column names don't match?

I am trying to use whereIn() to find records who have 'prov' that can be found in this array;
array:3 [▼
0 => array:6 [▼
"id" => 1
"contest_id" => 1
"contest_zone_id" => 1
"province" => "ON"
"created_at" => "2018-06-20 13:37:45"
"updated_at" => "2018-06-20 13:37:45"
]
1 => array:6 [▼
"id" => 2
"contest_id" => 1
"contest_zone_id" => 1
"province" => "SK"
"created_at" => "2018-06-20 13:37:45"
"updated_at" => "2018-06-20 13:37:45"
]
2 => array:6 [▼
"id" => 3
"contest_id" => 1
"contest_zone_id" => 1
"province" => "NB"
"created_at" => "2018-06-20 13:37:45"
"updated_at" => "2018-06-20 13:37:45"
]
]
So I can do this;
->whereIn('prov', $provinces)
However, in my 2 tables 'prov' and 'province' aren't the same so it's not working. Is there a way to explicitly declare which columns I want to compare?
Thanks!
UPDATE WITH FIX
Array needs to be pared down to just the field you are looking to match.

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

Resources