Multiple array null value? - foreach

I try to insert mutiple arrays per job_id when i try this my array values out put as null value this is my store function
$input output :
array:7 [▼
"date" => "2016-01-10",
"job_id" => "2",
"charge" => array:2 [▼
0 => "ch1",
1 => "ch2"
],
"category" => array:2 [▼
0 => "se1",
1 => "se2"
],
"amount" => array:2 [▼
0 => "100"
1 => "200"
]
]
Full Code :
$input = Input::all();
$charge = Input::get('charge');
$date = Input::get('date');
$job = Input::get('job_id');
$amount = Input::get('amount');
$cat = Input::get('category');
$id = \Auth::id();
$data = array();
foreach ($charge as $key=>$value ){
$data[] = [
'date' => $date,
'charge'=>$key['charge'],
'job_id'=>$job,
'user_id'=>$id,
'amount'=>$key['amount'],
'category'=>$key['category'],
];
}
dd($data);
When i try to get out put i will get this out put
array:2 [▼
0 => array:6 [▼
"date" => "2016-01-10"
"charge" => null
"job_id" => "2"
"user_id" => 1
"amount" => null
"category" => null
]
1 => array:6 [▼
"date" => "2016-01-10"
"charge" => null
"job_id" => "2"
"user_id" => 1
"amount" => null
"category" => null
]
]
What i want is i want insert data to my database any one suggest me good idea ?
Answer for this question from Laracast Check this link

Your final output looks correct, try to use the following code to insert the $data to your DB :
Model::insert($data); // Eloquent
DB::table('table')->insert($data); // Query Builder
You should construct your array using $inputs['amount'][$key] for amount value and $inputs['category'][$key] for category, like following :
foreach ($charge as $key=>$value ){
$data[] = [
'date' => $date,
'charge'=>$value,
'job_id'=>$job,
'user_id'=>$id,
'amount'=>$inputs['amount'][$key],
'category'=>$inputs['amount'][$key],
];
}
Hope this helps.

Related

Paginating messages with Microsoft Graph API v.1.0

I'm using Microsoft Graph API v.1.0 I'm trying to get the emails I only see 10 of them which I understand is the default pagination page limit but there is no #odata:nextLink property being returned to go to the next batch of results.
This is my code:
$graph = new Graph();
$graph->setAccessToken(session('access_token'));
$me = $graph->createRequest("get", "/me/messages")
->setReturnType(Model\User::class)
->execute();
return $me;
The result:
array:10 [▼
0 => User {#379 ▼
#_propDict: array:28 [▶]
}
1 => User {#365 ▶}
2 => User {#375 ▶}
3 => User {#368 ▶}
4 => User {#367 ▶}
5 => User {#366 ▶}
6 => User {#380 ▶}
7 => User {#373 ▶}
8 => User {#382 ▶}
9 => User {#383 ▼
#_propDict: array:28 [▼
"#odata.etag" => "W/"CQAAABYAAAC8b+tAO4nLRZCbkhud5CXFAANG4sRR""
"id" => "AAMkADdlZTBjNjQ4LWI0OGItNDFhZS05ZDNiLThiY2JkYzIzZWZkYwBGAAAAAABFX7lJCx7ZRLTJ6iI0yZK6BwC8b_tAO4nLRZCbkhud5CXFAAAAAAEKAAC8b_tAO4nLRZCbkhud5CXFAANGMK1oAAA="
"createdDateTime" => "2018-01-05T14:25:58Z"
"lastModifiedDateTime" => "2018-01-05T14:25:58Z"
"changeKey" => "CQAAABYAAAC8b+tAO4nLRZCbkhud5CXFAANG4sRR"
"categories" => []
"receivedDateTime" => "2018-01-05T14:25:58Z"
"sentDateTime" => "2018-01-05T14:25:58Z"
"hasAttachments" => false
"internetMessageId" => "<DB5PR05MB15093E653073F8E1A95F0EE2D11C0#DB5PR05MB1509.eurprd05.prod.outlook.com>"
"subject" => "test"
"bodyPreview" => "HTML"
"importance" => "normal"
"parentFolderId" => "AQMkADdlZQAwYzY0OC1iNDhiLTQxYWUtOWQzYi04YmNiZGMyM2VmZGMALgAAA0VfuUkLHtlEtMnqIjTJkroBALxv60A7ictFkJuSG53kJcUAAAIBCgAAAA=="
"conversationId" => "AAQkADdlZTBjNjQ4LWI0OGItNDFhZS05ZDNiLThiY2JkYzIzZWZkYwAQAN7fN0jAEi5LroDvBtBQMRs="
"isDeliveryReceiptRequested" => false
"isReadReceiptRequested" => false
"isRead" => true
"isDraft" => false
"webLink" => "https://outlook.office365.com/owa/?ItemID=AAMkADdlZTBjNjQ4LWI0OGItNDFhZS05ZDNiLThiY2JkYzIzZWZkYwBGAAAAAABFX7lJCx7ZRLTJ6iI0yZK6BwC8b%2BtAO4nLRZCbkhud5CXFAAAAAAEK ▶"
"inferenceClassification" => "focused"
"body" => array:2 [▶]
"sender" => array:1 [▶]
"from" => array:1 [▶]
"toRecipients" => array:1 [▶]
"ccRecipients" => []
"bccRecipients" => []
"replyTo" => []
]
}
]
Please see the PHP documentation for paging at https://github.com/microsoftgraph/msgraph-sdk-php/wiki/Example-calls#get-a-collection-of-items
It looks like you should change the return type to match the endpoint. In your case, your return type is User but you're querying for messages. That's why your result array has User objects. I think the model type you're looking for is Message and you can see the source for this at https://github.com/microsoftgraph/msgraph-sdk-php/blob/dev/src/Model/Message.php
I ended up using the API directly and formatting the next and previous links.
//get messages from inbox folder
$emails = Api::get("me/mailFolders/$inbox/messages?".http_build_query($messageQueryParams));
$previous = null;
$next = null;
$total = $emails['#odata.count'];
if (isset($emails['#odata.nextLink'])) {
$first = explode('$skip=', $emails['#odata.nextLink']);
$skip = explode('&', $first[1]);
$previous = $skip[0]-$offset;
$next = $skip[0];
if ($previous < 0) {
$previous = 0;
}
if ($next == $total) {
$next = null;
}
}

Yii2 sum with group by in gridview not working

Below is my query :
$query = AssignApplicant::find()
->select(['sum(total_pay) as sum','applicant.first_name','applicant.last_name'])
->joinWith(['position', 'applicant'])->orderBy(['job_positions.id' => SORT_DESC]);
$query->groupBy(['applicant_id']);
My grid view
GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'kartik\grid\SerialColumn'],
[
'attribute' => 'applicant',
'value' => 'applicant.first_name',
],
'sum'
],
]);
This print null for the applicant.first_name. While when I run the raw query in the db it works. When I remove the below line it show the first_name but not the sum
->select(['sum(total_pay) as sum','applicant.first_name','applicant.last_name'])
I solved by changing this :
->select(['sum(total_pay) as sum','`assign_applicant`.*'])

how do I search for a string value inside an array stored in a hash

I am using ruby on rails and trying to figure out if I can (and/or how I) search for a Hash within an Array for a specific string value? If there is a match (finds Bob), I want it to return True.
```
string_query = "Bob#gmail.com"
email_array is the following:
[
[0] {
"email" => "bill#gmail.com",
"name" => "william"
},
[1] {
"email" => "mike#gmail.com",
"name" => "michael"
},
[2] {
"email" => "Bob#gmail.com",
"name" => "robert"
}
]
```
I see this example on Stack Overflow - but it is numeric. Mine is a string.
How do I get a hash from an array based on a value in the hash?
Many thanks.
I have not tried this as I am out now, but this could work
string_query = "Bob#gmail.com"
email_array.each do |s|
if s['email'] == string_query
#your comparision statements here
end
end
string_query = "Bob#gmail.com"
email_array = [{
"email" => "bill#gmail.com",
"name" => "william"
},
{
"email" => "mike#gmail.com",
"name" => "michael"
},
{
"email" => "Bob#gmail.com",
"name" => "robert"
},
{
"email" => "Bob#gmail.com",
"name" => "robert2"
}
]
If you want to select all of the matching hashes you can use select
email_array.select {|hash| hash["email"] == string_query }
#=> [{"email"=>"Bob#gmail.com", "name"=>"robert"}, {"email"=>"Bob#gmail.com", "name"=>"robert2"}]
If you simply want to check for true or false, use any?
email_array.any? {|hash| hash["email"] == string_query }
#=> true
If you are only interested in the first instance. You can use detect
email_array.detect {|hash| hash["email"] == string_query }
#=> {"email"=>"Bob#gmail.com", "name"=>"robert"}
You could do this
email_array.select {|hash| hash["email"] == string_query }.present?

How do I pull out an object from an array that contains a key with a certain value

My question is how to I pull out a specific object from this array that has an 'id' with the specific value of 888?
[{ "token" => "1212",
"category" => "A",
"name" => "page 2",
"id" => "888"
},
{ "token" => "3434",
"category" => "B",
"name" => "page 1",
"id" => "999",
}]
I have tried find_by, where, and a whole host of other things.
You can try using "select" on the array:
arr.select {|k| k['id'] == "888" }
This will return an array containing all array elements where the condition is met.

What is an elegant way of modifying hashes inside an array within a nested hash in Ruby

I would like to transform this
def some_process(k,v)
return "#{v}_#{k}"
end
a_hash = {
"i_love_hashes" => {
"thing" => 20,
"other_thing" => "0",
"yet_another_thing" => "i disagree",
"_peculiar_thing" => [
{"count" => 30,
"name" => "freddie"},
{"count" => 15,
"name" => "johhno"},
{"count" => 12,
"name" => "mohammed"},
]
},
"as_do_i" => {
"thing" => 10,
"other_thing" => "2",
"yet_another_thing" => "i strongly agree",
"_peculiar_thing" => [
{"count" => 10,
"name" => "frodo"},
{"count" => 4,
"name" => "bilbo"},
{"count" => 2,
"name" => "elizabeth"},
]
}
}
into this
{
"i_love_hashes"=>{
"thing"=>20,
"other_thing"=>"0",
"yet_another_thing"=>"i disagree",
"_peculiar_thing"=> [
{"count"=>30, "name"=>"freddie", :sinister_name=>"freddie_i_love_hashes"},
{"count"=>15, "name"=>"johhno", :sinister_name=>"johhno_i_love_hashes"},
{"count"=>12, "name"=>"mohammed", :sinister_name=>"mohammed_i_love_hashes"}
]},
"as_do_i"=>{
"thing"=>10,
"other_thing"=>"2",
"yet_another_thing"=>"i strongly agree",
"_peculiar_thing"=>[
{"count"=>10, "name"=>"frodo", :sinister_name=>"frodo_as_do_i"},
{"count"=>4, "name"=>"bilbo", :sinister_name=>"bilbo_as_do_i"},
{"count"=>2, "name"=>"elizabeth", :sinister_name=>"elizabeth_as_do_i"}
]
}
}
this is the code I am currently using to achieve this
a_hash.each_with_object({}) do |(k,v),o|
o.merge!({k =>
v.each_with_object({}) do |(a,b),g|
g.merge!({ a =>
(b.is_a?(Array) ? b.collect {|x| x.merge({sinister_name: (some_process k, x["name"])})} : b)
})
end
})
end
Ignoring the specific details of what is being returned by "some_process" (what is important is that it depends on the outer most key and the inner name values, in this example), are there any alternatives that would be considered more elegant?
Why not do a recursive function?
def add_siniter(hash)
hash[:siniter_name] = "#{hash['name']}_i_love_hashes"
hash
end
def format_hash(item)
case item
when Hash then item.keys.each{|key| format_hash(item[key])}
when Array then item.map!{|h| add_siniter(h)}
end
end
format_hash(a_hash)

Resources