Add empty table to table with data - php

I am beginner. I make my project in PHP and Laravel 8.
I have this $array:
array:1 [▼
"attributes" => array:37 [▼
"id" => 1
"hash" => "19f149b6-f7e2-4d61-b3c5-d46ebc92f681"
"hidden_carrier_data" => 0
"delivery_name_surname" => null
"delivery_signature_binary" => null
"disable_change_status_driver" => 0
]
]
I need add OLD to this array:
array:1 [▼
"old" => array:0
"attributes" => array:37 [▼
"id" => 1
"hash" => "19f149b6-f7e2-4d61-b3c5-d46ebc92f681"
"hidden_carrier_data" => 0
"delivery_name_surname" => null
"delivery_signature_binary" => null
"disable_change_status_driver" => 0
]
]
How can I make it?
Please help me

You can try out
$array["old"] = [...something];
Let me know this worked or not.

Related

Convert Collection to Array with I=ID in Laravel

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();

Having issues with getting the candidate with the highest vote and his name using laravel 7

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'

How to store the quantity in the $regTypes array?

I want to have an array with the info about each registration type associated with a registration. For example if for the registration with id "1" the user selected two registration types of type "General" and one of the type "Plus" the $regTypes array should have this content with 2 item:
'registrationTypes' => [
[
'name' => 'general',
'price' => '5',
'quantity' => '2'
],
[
'name' => 'plus',
'price' => '10',
'quantity' => '1'
]
]
The registration_types table have the name and the price. So I have this query to get some info about a registration in a conference.
$registration = Registration
::with('conference', 'Conference.registrationTypes')->where('id', 1)->first();
And then I have this code to create the array with the necessary info:
$regTypes = [];
foreach($registration->conference->registrationTypes as $key=>$registrationType){
$regTypes [
'regType' => [
'name' => $registration->conference->registrationTypes[$key]['name'],
'price' => $registration->conference->registrationTypes[$key]['price']
]
];
}
My doubt is how to also store the quantity in the $regTypes array. Because the quantity is not stored in the database.
Maybe to get the quantity is necessary to do antoher query. With this code below:
$registrationTypeDetails = Registration::with('participants:id,registration_type_id,registration_id')->find($regID);
//dd($registrationTypeDetails);
$type_counts = [];
foreach ($registrationTypeDetails->participants as $p) {
$name = $p->registration_type->name;
if (!isset($type_counts[$name])) {
$type_counts[$name] = 0;
}
$type_counts[$name]++;
}
dump($type_counts);
The $type_counts shows the quantity of each registration type associated with the registration:
array:2 [▼
"general" => 2
"plus" => 1
]
Do you know how to use this $type_counts content to store the quantity properly in the $regTypes array?
To directly answer your question (IE not change the controller code, just "use this $type_counts content to store the quantity properly in the $regTypes array"), I'm hopeful this should work for you:
$regTypes = [];
foreach($registration->conference->registrationTypes as $key=>$registrationType){
$typeName = $registration->conference->registrationTypes[$key]['name'];
$regTypes [
'regType' => [
'name' => $typeName,
'price' => $registration->conference->registrationTypes[$key]['price'],
'quantity' => $type_counts[$typeName]
]
];
}
Basically just pulling the count from the $type_counts array based on the name of the Registration Type being the key that you added in the foreach ($registrationTypeDetails->participants as $p) loop.
Depending upon what you are after, it might be easier just to loop on the registration types, rather than go through $registration->conference->registrationTypes. This way you don't have duplicates. But that assumes you don't want duplicates :)
I hope this code useful for you .If you need the number of participants in any type, you can use the following code:
$registration = Registration::with('conference','Conference.registrationTypes','Conference.registrationTypes.participants')
->where('id',$regID)->first();
$result=$registration->conference->registrationTypes->each(function ($item, $key) use($registration) {
$item['try_count']=$item->participants->count();
});
dd($result->toArray());
I added the try_count variable to the final result:
array:2 [▼
0 => array:6 [▼
"id" => 1
"name" => "general"
"price" => 0
"conference_id" => 1
"try_count" => 8
"participants" => array:8 [▼
0 => array:5 [▶]
1 => array:5 [▶]
2 => array:5 [▶]
3 => array:5 [▶]
4 => array:5 [▶]
5 => array:5 [▶]
6 => array:5 [▶]
7 => array:5 [▶]
]
]
1 => array:6 [▼
"id" => 2
"name" => "plus"
"price" => 1
"conference_id" => 1
"try_count" => 5
"participants" => array:5 [▼
0 => array:5 [▶]
1 => array:5 [▶]
2 => array:5 [▶]
3 => array:5 [▶]
4 => array:5 [▶]
]
]
]

Laravel: Create Array

I have a multi questions with multi answers on the same page and I want to display all data so I create a new array to pass it to the view the new array will contain all question with the answers if the question has no answer then the value will be null and if it has answer the value should be as the value that stored on the database.the problem that the new array stores the last question data only.
$exist_data = RatingDatum::where($check_data)->get(); /*get all the previous data I have to answers here for the two questions only the third question have no answer */
$datum = array();
] foreach ($question_angles as $index => $angle) {
if (!empty($exist_data)){
foreach ($exist_data as $row) {
if ( $row['question_angle_id'] == $angle->id) {
$datum[$index]['question_angle_id'] = $angle->id;
$datum[$index]['answer_id'] = $row['answer_id'];
$datum[$index]['comment'] = $row['comment'];
$datum[$index]['reference'] = $row['reference'];
} else {
$datum[$index]['question_angle_id'] = $row['question_angle_id'];
$datum[$index]['answer_id'] = $row['answer_id'];
$datum[$index]['comment'] = null;
$datum[$index]['reference'] = null;
}
}
}
}
dd($datum);
array:3 [▼
0 => array:4 [▼
"question_angle_id" => 2
"customize_question_id" => null
"comment" => null
"reference" => null
]
1 => array:4 [▼
"question_angle_id" => 2
"customize_question_id" => 7666
"comment" => "test"
"reference" => "test"
]
2 => array:4 [▼
"question_angle_id" => 2
"customize_question_id" => null
"comment" => null
"reference" => null
]
]
the data should look like this the first two question have answers but the last question not have answer so the values is null:
array:3 [▼
0 => array:4 [▼
"question_angle_id" => 1
"answer_id" =>2
"comment" => null
"reference" => null
]
1 => array:4 [▼
"question_angle_id" => 2
"answer_id" => 6
"comment" => "test"
"reference" => "test"
]
2 => array:4 [▼
"question_angle_id" => 3
"answer_id" => null
"comment" => null
"reference" => null
]
]

How to get first element from a sub array

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']);

Categories