I have a deployments Laravel Collection like this:
Illuminate\Support\Collection {#415 ▼
#items: array:5 [▼
0 => array:7 [▼
"id" => 31
"status" => "active"
"name" => "Deployment 1"
"spots" => array:4 [▼
0 => array:2 [▼
"id" => 33
"status" => "active" <-- Want to change this
]
1 => array:2 [▶]
2 => array:2 [▶]
3 => array:2 [▶]
]
"data" => array:3 [▶]
]
1 => array:7 [▶]
2 => array:7 [▶]
3 => array:7 [▶]
4 => array:7 [▶]
]
}
I want to update the nested status value to inactive. I have used the Laravel map function, but it only seems to work on collections that have one nesting level. So this...
$this->deployments->map(function ($deployment) {
$deployment['spots'][0]['status'] = 'inactive';
});
dd($this->deployments);
...leaves $this->deployments untouched.
Also tried using nested map functions obtaining a Call to a member function map() on array exception on second level as second and next nesting levels are considered arrays...
Any thoughts?
Thanks in advance.
With the map method, you are almost there. You will have to return the change made in $deployment and do an ->all() at the end to update the collection with the modified values.
For updating a single spot:
$deployments = $deployments->map(function($deployment){
$deployment['spots'][0]['status'] = 'inactive';
return $deployment;
})->all();
For updating all spots:
$deployments = $deployments->map(function($deployment){
foreach($deployment['spots'] as &$spot){
$spot['status'] = 'inactive';
}
return $deployment;
})->all();
For anyone researching this, a more elegant solution might be:
For updating a single spot:
$data = $deployments->all();
$deployments = data_set($data, 'spots.0.status', 'inactive');
For updating all spots:
$data = $deployments->all();
$deployments = data_set($data, 'spots.*.status', 'inactive');
Related
I need to add pagination, but it seems complicated in my case, because I get data from database with paginate, but then I modify this data and when I call links() method on the blade, I get the following exception
Method Illuminate\Support\Collection::links does not exist.
My code in the Controller method:
$transactionsByLastMonth = Transaction::where('created_at', '>=', Carbon::now()->subDays(30)->toDateTimeString())
->where('user_id', Auth::user()->id)
->with(['user', 'propertyFrom', 'propertyTo', 'paddockFrom', 'paddockTo', 'cattleTypeFrom', 'cattleTypeTo'])
->paginate(10);
$transactionsByDays = collect(TransactionResource::collection($transactionsByLastMonth))
->sortByDesc('created_at')
->groupBy(function($date) {
return Carbon::parse($date['created_at'])->format('d');
});
return view('user.reports.index', compact('transactionsByDays'));
Yes, a pagination limits my data to 10 rows, but due to I'm grouping data by days, my collection modifies itself and I can't use $transactionsByDays->links() method to show pagination.
If see dd($transactionsByDays) , it looks like:
Illuminate\Support\Collection {#1381 ▼
#items: array:3 [▼
"01" => Illuminate\Support\Collection {#1019 ▼
#items: array:7 [▼
0 => array:16 [▶]
1 => array:16 [▶]
2 => array:16 [▶]
3 => array:16 [▶]
4 => array:16 [▶]
5 => array:16 [▶]
6 => array:16 [▶]
]
}
31 => Illuminate\Support\Collection {#1386 ▼
#items: array:2 [▼
0 => array:16 [▶]
1 => array:16 [▶]
]
}
30 => Illuminate\Support\Collection {#1384 ▼
#items: array:1 [▼
0 => array:16 [▶]
]
}
]
}
Therefore I need to paginate all arrays together which inside "01", 31, 30...
How can I do it?
Maybe rewrite code above in the controller somehow?
The main aim is that I need to group data to every day according to my json-resource class.
Any ideas?
I've found a solution gyus. I had to pass also a variable transactionsByLastMonth and use links() method over this.
In the meantime I use foreach on the my blade file over transactionsByDays variable. It's correctly works together cause it uses the same data from database, just in the first case its not filtered and not grouped and in the second one it is.
return view('user.reports.index', compact('transactionsByLastMonth', 'transactionsByDays'));
I have Project and Country Model. There is a many to many relations. I get projects with countries. Result is below
array:5 [▼
0 => array:5 [▼
"id" => 2
"account_id" => 1
"start_date" => "Jul 2012"
"end_date" => "Aug 2013"
"countries" => array:1 [▼
0 => array:2 [▼
"id" => 148
"pivot" => array:2 [▶]
]
]
]
1 => array:5 [▶]
2 => array:5 [▶]
3 => array:5 [▶]
4 => array:5 [▶]
]
I wont to get all unique countries count. In the now I do it with this way
$projects->pluck('countries')->collapse()->pluck('id')->unique()->count()
Question. Can I use pluck with nested relation column and has any more shortly and good solution?? for example like this
$projects->pluck('countries.id')->count();
You can use this:
$projects->pluck('countries.*.id')->flatten()->unique()->count()
You can't use:
$projects->pluck('countries.id')...
Because countries is an array of arrays.
But you can use the 'countries.*.id' on those cases
Or the other way round...
Country::whereHas('projects', function ($query) {
// $query->where(); if you want to limit the projects
})->count();
You get unique countries since your fetching from the countries table
I find this solution
$projects->groupBy('countries.*.id')->count();
I've created a collection like this :
Collection {#651 ▼
#items: array:3 [▼
0 => array:3 [▼
"orderId" => "402457"
"orderCreated" => DateTime {#656 ▶}
"foods" => array:2 [▶]
]
1 => array:3 [▼
"orderId" => "402457"
"orderCreated" => DateTime {#661 ▶}
"foods" => array:2 [▶]
]
2 => array:3 [▼
"orderId" => "402457"
"orderCreated" => DateTime {#665 ▶}
"foods" => array:2 [▶]
]
]
}
I demand to achieve collection like this (with Laravel collection):
Collection {#651 ▼
#items: array:3 [▼
0 => array:3 [▼
"orderId" => "402457"
"orderCreated" => DateTime {#656 ▶}
"foods" => array:6 [▶]
]
]
}
Because orderId and orderCreated are all the same in arrays. I need to make single array that collect orderId and orderCreated with all foods.
Any suggestion?
You can take the data from the first one and just combine all of the food items. For instance like this:
$new = collect([
'orderId' => $old->first()->orderId,
'orderCreated' => $old->first()->orderCreated,
'foods' => $old->pluck('foods')->flatten(1),
]);
The exact implementation will depend on how you built your initial collection.
I am trying to get data in from multidimensional array without using foreach
i tried using in_array() function but not worked
$abc = array()
in_array($abc , $private_job->cities)
in_array() expects parameter 2 to be array, string given
on using $private_job->cities got the following result
Collection {#408 ▼
#items: array:2 [▼
0 => city {#416 ▼
+wasRecentlyCreated: false
#attributes: array:2 [▼
"id" => 7
"city_name" => "Gujranwala"
]
}
1 => city {#417 ▼
+wasRecentlyCreated: false
#attributes: array:2 [▶]
#original: array:4 [▼
"id" => 4
"city_name" => "Islamabad"
"pivot_private_jobabd_id" => 53
"pivot_city_id" => 4
]
}
]
}
whereas i am interested in getting
"id" => 7
"id" => 4
in an an array
$result = $private_job->cities->map(function($data){
return $data['id'];
})->all();
So I'm trying to make a function that searches a string in a whole array. But it give me anything... So my array looks like this :
array:1 [▼
"list" => array:2 [▼
"pagination" => array:5 [▶]
"entries" => array:11 [▼
0 => array:1 [▼
"entry" => array:8 [▼
"firstName" => "Doctor"
"lastName" => "Who"
"emailNotificationsEnabled" => true
"telephone" => "0123456789"
"company" => []
"id" => "DW"
"enabled" => true
"email" => "doctorwho#time.lord"
]
]
1 => array:1 [▶]
2 => array:1 [▶]
3 => array:1 [▶]
4 => array:1 [▶]
5 => array:1 [▶]
6 => array:1 [▶]
7 => array:1 [▶]
8 => array:1 [▶]
9 => array:1 [▶]
10 => array:1 [▶]
]
]
]
So for exemple at first I did $key = array_search("doctor", $users); but this gives me nothing. So I thought it was because I have a multidimensional array. So I reduced it to just one array (and I would search in the rest of the original array with a for loop), so now I'm working with this array that I got with $users['list']['entries'][0]
array:1 [▼
"entry" => array:8 [▼
"firstName" => "Doctor"
"lastName" => "Who"
"emailNotificationsEnabled" => true
"telephone" => "0123456789"
"company" => []
"id" => "DW"
"enabled" => true
"email" => "doctorwho#time.lord"
]
]
But $key = array_search("doctor", $users['list']['entries'][0]); still doesn't give me anything (but false).
Does anyone know where is my mistake ? Because I couldn't find a solution of my problem yet and it's been a pretty long time I'm on it... I'm still a beginner in php so maybe I've missed something obvious and i'm sorry if I did.
Thank you in advance !
you are searching for doctor,but value stored in your array is as Doctor. So either search for Doctor as
$key=array_search('Doctor', $users['list']['entries'][0]));
or for case in-sensetive search use
$key=array_search(strtolower('Doctor'), array_map('strtolower', $users['list']['entries'][0]));
Note: as your array is multidimensional, so use loop to search your value