I am using the maatwebsite to import excel to db and
$dataArray[] =
[
'name' => $row['name']
'email' => $row['email'],
];
Apprentice::insert($dataArray);
When the variable is printed the result is:
array:2 [▼
0 => array:18 [▼
"name" => "Maicol Stiven"
"email" => "maic1ce#live.com"
]
1 => array:18 [▼
"name" => "Cristian Camilo"
"email" => "carin45#gmail.com"
]
]
the email is a unique, I need when the email is duplicated, omit it and insert the other records
How can I do it? thanks
Use the firstOrNew method like so
$a = Apprentice::firstOrNew(['email' => $row['email']]);
$a->name = $row['name'];
$a->save();
Related
I have the following multi dimension array and I am not able to do a foreach loop (with laravel). I want to show the name.
Any idea how to loop trough that array to show just the name? I reduced the showed array -> ...
I want to loop trough that array not in a view but in a controller because i want to create a database entry for every client
array:1 [▼
"client" => array:52 [▼
0 => array:11 [▼
"name" => "Company One"
...
]
1 => array:11 [▼
"name" => "Company 2"
...
]
Thanks for your help.
$array = [
'client' => [
[
'name' => 'Company One',
'foo' => 'Foo One',
],[
'name' => 'Company 2',
'foo' => 'Foo 2',
]
]
];
$names = array_pluck($array['client'], 'name');
foreach($names as $name) {
echo $name; // Replace this with the logic to create DB entry
}
Its easy all you have to do is this ,lets assume that your array are in the varibale $myArray
$myArray = [▼
"client" => array:52 [▼
0 => array:11 [▼
"name" => "Company One"
...
]
1 => array:11 [▼
"name" => "Company 2"
...
]
then you have to do:
#foreach ($myArray->client as $data)
{{$data->name}}
#endforeach
I want to get the quantity of each registration type with the code below:
$registrationTypeDetails = Registration::with('participants:id,registration_type_id,registration_id')->find($regID);
$type_counts = [];
foreach ($registrationTypeDetails->participants as $p) {
$name = $p->registration_type->name;
if (!isset($type_counts[$name])) {
$type_counts[$name] = 0;
}
$type_counts[$name]++;
}
The dd($type_counts) if the user is doing a registraton with 2 registration types "general" and one registration type "plus" shows:
array:2 [▼
"geral" => 2
"plus" => 1
]
So it's working fine. But then I need to make a post request to an API where is necessary to send in the request body the quantity of each registration type, in this case for the registration type "general" the value should be "2" and for the registration type "plus" the value should be "1".
But it's not working properly, the value is always "1" for both registration types.
Do you know where is the issue?
foreach($registration->conference->registrationTypes as $key=>$registrationType){
$items['invoice']['items'][] = [
'name' => $registration->conference->registrationTypes[$key]['name'],
'unit_price' => $registration->conference->registrationTypes[$key]['price'],
'quantity' => $type_counts[$name],
];
}
$create = $client->request('POST', 'https://...', [
'query' => ['api_key' => '...'], 'json' => $items,
]);
The result array has the quantity "1" like below but the quantity should be "2" for registration type "general" and "1" for "plus":
array:1 [▼
"invoice" => array:4 [▼
"client" => array:7 [▶]
"items" => array:2 [▼
0 => array:5 [▼
"name" => "general"
"unit_price" => 10
"quantity" => 1
]
1 => array:5 [▼
"name" => "plus"
"unit_price" => 0
"quantity" => 1
]
]
]
]
$name is never changed in the registration types loop:
// the $name index is always the same
'quantity' => $type_counts[$name],
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 [▶]
]
]
]
I have a form request which I need to validate . If I dd the $request->all() it shows me the following result.
"adults_information" => array:1 [▼
0 => array:6 [▼
"first_name" => "Luke"
"last_name" => "Greer"
"dob_day" => "08"
"dob_month" => "01"
"dob_year" => 1935
"gender" => "M"
]]
"contact_name" => "Eula Dennis"
"mobile_number" => "7308001726"
What I want is to create extra field after dob_year such as dob which constist of calculation of dob_day,"dob_year","dob_month" . I want some line of code such that when I do dd($request->all()) . I want to get the output like this .
"adults_information" => array:1 [▼
0 => array:6 [▼
"first_name" => "Luke"
"last_name" => "Greer"
"dob_day" => "08"
"dob_month" => "01"
"dob_year" => 1935
"gender" => "M",
"dob"=>"1935-01-08"
]]
"contact_name" => "Eula Dennis"
"mobile_number" => "7308001726"
I tried $request->add() but it didn't work . Any help will be appriciated
The correct syntax is not $request->add but $request->request->add.
So:
$request->request->add([
'adults_information'=>$request->adults_information + ['dob' => '1935-01-08']
]);
$inputs = $request->all();
foreach($inputs['adults_information'] as $key => $info)
{
$dob = $info['dob_year'].'-'.
$info['dob_month'].'-'.
$info['dob_day'];
$inputs['adults_information'][$key]['dob'] = $dob;
}
$request->merge($inputs);
dd($request->all());
Hi u can use merge() with array_push to push a nested array.
$adults_information = $request->adults_information;
$insert = [
"first_name" => "Luke",
"last_name" => "Greer",
"dob_day" => "08",
"dob_month" => "01",
"dob_year" => 1935,
"gender" => "M",
"dob"=>"1935-01-08"
];
array_push($adults_information, $insert);
$request->merge('adults_information', $adults_information);
https://laravel.com/docs/5.6/requests
Hope this helps
You can use replace method for appending item to request object. For more details you can check Laravel API docs https://laravel.com/api/5.6/Illuminate/Http/Request.html#method_replace . For example.
$data = $request->all();
$data['appending_data_1'] = 'dummy value';
$data['appending_data_2'] = 'dummy value';
$request->replace($data);
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 :)