I am beginner php developer.
I have small problem with my array. I use in my project Laravel 7
I have this code:
$items = collect($discount->products->toArray());
It's result me:
Illuminate\Support\Collection {#1783 ▼
#items: array:2 [▼
0 => array:17 [▼
"id" => 1
"product_category_id" => 5
"image_id" => null
"vat_type_id" => 1
"name" => "Produkt 1"
"slug" => "produkt-1"
"lead" => "<p>fewferfer</p>"
"description" => "<p> </p>"
"price" => "123.00"
"loyalty_program_points_price" => 2
"min_student_level" => null
"marked_as_available_at" => "2020-09-30T11:45:51.000000Z"
"deactivated_at" => null
"created_at" => "2020-09-30T11:45:23.000000Z"
"updated_at" => "2020-09-30T11:45:51.000000Z"
"deleted_at" => null
"pivot" => array:3 [▶]
]
1 => array:17 [▼
"id" => 2
"product_category_id" => 5
"image_id" => null
"vat_type_id" => 1
"name" => "Produkt 2"
"slug" => "produkt-2"
"lead" => "<p> </p>"
"description" => "<p>fergfer</p>"
"price" => "21.00"
"loyalty_program_points_price" => 3
"min_student_level" => null
"marked_as_available_at" => "2020-09-30T11:45:38.000000Z"
"deactivated_at" => null
"created_at" => "2020-09-30T11:45:38.000000Z"
"updated_at" => "2020-09-30T11:45:38.000000Z"
"deleted_at" => null
"pivot" => array:3 [▶]
]
]
}
I need convert it to this result:
Array{
1 => 1 (id)
2 => (id)
}
How can I make it?
I need array with only id values
Collections have the pluck method, which allows you to grab all the values of a specific key. To get all of the ids, you'd just need to do
$ids = $items->pluck('id')->all();
If you want them to have the same key as the id as well, pass that as the second parameter
$ids = $items->pluck('id', 'id')->all();
As a note, if products is a relation, then it's already a collection. You do not need to convert it to an array, then back to a collection:
$product_ids = $discount->products->pluck('id')->all();
I Am trying to get from the candidate with the highest votes and his name also, all have tried runs false. I just dd() this below, after getting all the presidential candidates.
Illuminate\Database\Eloquent\Collection {#288 ▼
#items: array:4 [▼
0 => App\Candidate {#289 ▼
#connection: "mysql"
#table: "candidates"
#primaryKey: "id"
#keyType: "int"
#attributes: array:9 [▼
"id" => 1
"name" => "Brian"
"seat" => "president"
"regno" => "DIT-C004-0536\2013"
"votes" => 0 // this is the first candidate record, others are below.
"user_id" => 2
"created_at" => "2017-05-12 08:43:58"
"updated_at" => "2017-05-12 08:43:58"
"image" => ""
]
}
1 => App\Candidate {#290 ▼
#attributes: array:9 [▼
"id" => 5
"name" => "Juma"
"seat" => "president"
"regno" => "DIT-C004-0516\2013"
"votes" => 3
"user_id" => 7
"created_at" => "2017-05-12 09:03:03"
"updated_at" => "2017-05-12 14:48:54"
"image" => ""
]
}
2 => App\Candidate {#291 ▼
#attributes: array:9 [▼
"id" => 9
"name" => "Stephen"
"seat" => "president"
"regno" => "0001"
"votes" => 4 //trying to get the highest votes with the name.
"user_id" => 12
"created_at" => "2020-03-17 15:27:47"
"updated_at" => "2020-03-21 19:18:39"
"image" => ""
]
}
3 => App\Candidate {#292 ▼
#attributes: array:9 [▼
"id" => 10
"name" => "Gobby"
"seat" => "president"
"regno" => "DIT-C004-0436\2014"
"votes" => 0
"user_id" => 11
"created_at" => "2020-03-17 15:27:59"
"updated_at" => "2020-03-17 15:27:59"
"image" => ""
]
}
I have been searching for solutions all around but couldn't find one, Thanks in Advance.
This is my controller:
{
public function highestvote(){
$getpres = Candidate::where('seat','president')->get();
dd($getpres);
foreach($getpres as $getpresvote){
for($i = 0; $i < $getpresvote; $i++){
$highv = $getpresvote[$i];
}
}
return view('election.winners')->with('getpresvotes',$getpresvotes);
}
}
I die dump the code before looping, cause when I loop it keeps on giving me 0 or sometimes tells me stuff like array can't be converted int.
Your Controller Code should be like this in this case:
$seat = 'president';
$name= 'gobby';
$data = Candidate::where('seat', $seat)
->where('name', $name)
->orderBy('votes','desc')
->first()
print_r($data);
It will give you the maximum votes data with the seat 'president' and name 'gobby'
I'm using Laravel Eloquent to pick out unique answers.
So in my Database I have 3 answers, But they are under two different questions.
So all I would need returning back is the number 2, Not the number 3.
My code so far is :
$foundation_scores = Foundation::where('user_id', $this->user_id)
->where('foundation_section', 'theory')
->get();
$foundation_scores->unique('foundation_question');
$foundation_scores = count($foundation_scores);
The unique('foundation_question') is returning 3. It should return 2
Any ideas why this would be?
My collection looks as follows (NOTE) I've outputted it as an array to avoide all the code Laravel Returns :
array:3 [
0 => array:12 [
"foundation_id" => 3
"user_id" => 17019
"foundation_section" => "theory"
"unit_id" => 0
"foundation_sub_section" => "Unit 1"
"foundation_question" => 0
"foundation_reference" => "Ref one way st"
"foundation_answer" => "Ref answer st"
"created_at" => "2017-10-25 11:57:05"
"updated_at" => "2017-10-25 11:57:05"
"deleted_at" => null
"friendly_date" => "25th October 2017"
]
1 => array:12 [
"foundation_id" => 4
"user_id" => 17019
"foundation_section" => "theory"
"unit_id" => 0
"foundation_sub_section" => "Unit 1"
"foundation_question" => 0
"foundation_reference" => "Red"
"foundation_answer" => "Ans"
"created_at" => "2017-10-25 12:02:01"
"updated_at" => "2017-10-25 12:02:01"
"deleted_at" => null
"friendly_date" => "25th October 2017"
]
2 => array:12 [
"foundation_id" => 5
"user_id" => 17019
"foundation_section" => "theory"
"unit_id" => 0
"foundation_sub_section" => "Unit 1"
"foundation_question" => 1
"foundation_reference" => "XY"
"foundation_answer" => "Z"
"created_at" => "2017-10-25 12:02:19"
"updated_at" => "2017-10-25 12:02:19"
"deleted_at" => null
"friendly_date" => "25th October 2017"
]
]
Is their a bug with unique. Or is it something I'm doing wrong?
$foundation_scores->unique('foundation_question'); is not assigning unique values to any new variable. Also unique() does not change original collection.
So:
$unique_collection = $foundation_scores->unique('foundation_question');
echo count($unique_collection);
Question background
Hello, I have the following array of movie crew members:
array:7 [▼
0 => array:6 [▼
"credit_id" => "52fe49dd9251416c750d5e9d"
"department" => "Directing"
"id" => 139098
"job" => "Director"
"name" => "Derek Cianfrance"
"profile_path" => "/zGhozVaRDCU5Tpu026X0al2lQN3.jpg"
]
1 => array:6 [▼
"credit_id" => "52fe49dd9251416c750d5ed7"
"department" => "Writing"
"id" => 139098
"job" => "Story"
"name" => "Derek Cianfrance"
"profile_path" => "/zGhozVaRDCU5Tpu026X0al2lQN3.jpg"
]
2 => array:6 [▼
"credit_id" => "52fe49dd9251416c750d5edd"
"department" => "Writing"
"id" => 132973
"job" => "Story"
"name" => "Ben Coccio"
"profile_path" => null
]
3 => array:6 [▼
"credit_id" => "52fe49dd9251416c750d5ee3"
"department" => "Writing"
"id" => 139098
"job" => "Screenplay"
"name" => "Derek Cianfrance"
"profile_path" => "/zGhozVaRDCU5Tpu026X0al2lQN3.jpg"
]
4 => array:6 [▼
"credit_id" => "52fe49dd9251416c750d5ee9"
"department" => "Writing"
"id" => 132973
"job" => "Screenplay"
"name" => "Ben Coccio"
"profile_path" => null
]
5 => array:6 [▼
"credit_id" => "52fe49dd9251416c750d5eef"
"department" => "Writing"
"id" => 1076793
"job" => "Screenplay"
"name" => "Darius Marder"
"profile_path" => null
]
11 => array:6 [▼
"credit_id" => "52fe49de9251416c750d5f13"
"department" => "Camera"
"id" => 54926
"job" => "Director of Photography"
"name" => "Sean Bobbitt"
"profile_path" => null
]
]
As you can see this is a list of credits I'm getting via the TMDb API. The first step of building the above array was to filter out all jobs that I don't want to display, here's how I did that:
$jobs = [ 'Director', 'Director of Photography', 'Cinematography', 'Cinematographer', 'Story', 'Short Story', 'Screenplay', 'Writer' ];
$crew = array_filter($tmdbApi, function ($crew) use ($jobs) {
return array_intersect($jobs, $crew);
});
My question
I'd like to figure out how to take the above result one step further and combine jobs where the id is the same, so as to end up with something like this, for example:
array:7 [▼
0 => array:6 [▼
"credit_id" => "52fe49dd9251416c750d5e9d"
"department" => "Directing"
"id" => 139098
"job" => "Director, Story, Screenplay"
"name" => "Derek Cianfrance"
"profile_path" => "/zGhozVaRDCU5Tpu026X0al2lQN3.jpg"
]
I have also considered ditching doing this in my logic and instead doing it in my blade template, but I'm not sure how to achieve that.
How would you accomplish this?
You could nicely use Laravel's Collection in such a situation, which has a great number of methods which will help you in this case.
First, turn this array (the one you already filtered on jobs) to a Collection:
$collection = collect($crew);
Second, group this Collection by it's ids:
$collectionById = $collection->groupBy('id');
Now, the results are grouped by the id and transformed to a Collection in which the keys correspond to the id, and the value an array of 'matching' results. More info about it here.
Finally, just a easy script that iterates through all the results for each id and combines the job field:
$combinedJobCollection = $collectionById->map(function($item) {
// get the default object, in which all fields match
// all the other fields with same ID, except for 'job'
$transformedItem = $item->first();
// set the 'job' field according all the (unique) job
// values of this item, and implode with ', '
$transformedItem['job'] = $item->unique('job')->implode('job', ', ');
/* or, keep the jobs as an array, so blade can figure out how to output these
$transformedItem['job'] = $item->unique('job')->pluck('job');
*/
return $transformedItem;
})->values();
// values() makes sure keys are reordered (as groupBy sets the id
// as the key)
At this point, this Collection is returned:
Collection {#151 ▼
#items: array:4 [▼
0 => array:6 [▼
"credit_id" => "52fe49dd9251416c750d5e9d"
"department" => "Directing"
"id" => 139098
"job" => "Director, Story, Screenplay"
"name" => "Derek Cianfrance"
"profile_path" => "/zGhozVaRDCU5Tpu026X0al2lQN3.jpg"
]
1 => array:6 [▼
"credit_id" => "52fe49dd9251416c750d5edd"
"department" => "Writing"
"id" => 132973
"job" => "Story, Screenplay"
"name" => "Ben Coccio"
"profile_path" => null
]
2 => array:6 [▼
"credit_id" => "52fe49dd9251416c750d5eef"
"department" => "Writing"
"id" => 1076793
"job" => "Screenplay"
"name" => "Darius Marder"
"profile_path" => null
]
3 => array:6 [▼
"credit_id" => "52fe49de9251416c750d5f13"
"department" => "Camera"
"id" => 54926
"job" => "Director of Photography"
"name" => "Sean Bobbitt"
"profile_path" => null
]
]
}
Note: to use this Collection as an array, use:
$crew = $combinedJobCollection->toArray();
There are multiple ways to achieve this, for example: search the array for overlapping id's, but I think this is the easiest way to achieve this.
Goodluck!
Since you are trying to edit the array elements and its size, I believe array_map() or array_filter() won't be a solution to this.
This is what I could come up with...
$jobs = [
'Director', 'Director of Photography', 'Cinematography',
'Cinematographer', 'Story', 'Short Story', 'Screenplay', 'Writer'
];
$crew = [];
foreach($tmdbApi as $key => $member) {
if($member['id'] == $id && in_array($member['job'], $jobs)) {
if(!isset($crew[$key])) {
$crew[$key] = $member;
} else {
$crew_jobs = explode(', ', $crew[$key]['job']);
if(!in_array($member['job'], $crew_jobs)) {
$crew_jobs[] = $member['job'];
}
$crew[$key]['job'] = implode(', ', $crew_jobs);
}
}
}
Hope this answers your question :)
I am trying to get the first element from a subarray. The data needed in my result collection looks like this:
#attributes:array:11 [▼
"id" => 1
"title" => "Eerst nieuwsbericht voor Jochen"
"content" => "<p>Dit is een test</p>\n"
"tags" => ""
"images" => "[2,3,4,1]"
"social_media" => ""
"publish_date" => "2015-08-27 01:40:17"
"created_at" => "2015-08-27 13:40:17"
"updated_at" => "2015-08-27 13:40:17"
"slug" => "eerst-nieuwsbericht-voor-jochen"
"files" => array:4 [▼
2 => array:11 [▼
"id" => 2
"parent_id" => 0
"type" => "file"
"path" => "/uploads/flyfish"
"name" => "05-vogelkers1-E2.jpg"
"file_type" => "jpg"
"size" => 162936
"width" => 1024
"height" => 768
"created_at" => Carbon {#368 ▶}
"updated_at" => Carbon {#338 ▶}
]
3 => array:11 [▶]
4 => array:11 [▶]
1 => array:11 [▶]
]
]
I want to get the 'name' from the first element in the sub array 'files'. Is there a shorthand notation to do so?
I can get to my needed result with
$newsDetail->files[2]['name']
But then I need to know that key 2 is actually the first key in the array 'files'.
reset() rewinds array's internal pointer to the first element and
returns the value of the first array element.
So i believe in your case:
var_dump(reset($newsDetail->files)['name'])
Simply do:
current($newsDetail['files']);