Display contents of a hash if value exists - ruby

I have a hash:
req = {
"count" => 50100,
"results" => [
{"listing_id" => 615929315, "state" => "active", "user_id" => 140604756, "category_id" => 69150367},
{"listing_id" => 615929311, "state" => "active", "user_id" => 152528025, "category_id" => 69150367}
]
}
I want to find and display the entire internal hash if a particular user_id exists. I can find it:
req["results"][0].select{|key, value| value == 152528025}
# => {"user_id" => 152528025}
How do I then display this entire (nested) hash?
{"listing_id" => 615929311, "state" => "active", "user_id" => 152528025, "category_id" => 69150367}

req["results"].select{|x| x["user_id"] == 152528025}

Related

laravel model object changed unexpectedly while doing map operation on it

This is some wierd situation in which I am there right now like I have one object
$this->remitter;
if I dump this I get this
App\Models\IpayAppBc\Remitter {#1960
#connection: "ipay_app_bc__write"
+table: "remitter"
+timestamps: false
#primaryKey: "id"
+wasRecentlyCreated: false
#escapeWhenCastingToString: false
#attributes: array:15 [
"id" => 15016478
"name" => "ASKHSYA"
"name_last" => ""
"dob" => null
"mobile" => "1234567"
"limit_expiry" => "{"PB":"202212"}"
"address" => "PANCHKULA PANCHKULA HARYANA"
"city" => "PANCHKULA"
"pincode" => "134117"
"state" => "HARYANA"
"two_factor" => 0
"status" => 0
"otp_status" => 2
"paytm_key" => "h00097ea42dc-a6ed-42bc-b2cf-3b6a876f878b"
"ab_kyc" => 0
]
#original: array:15 [
"id" => 15016478
"name" => "ASKHSYA"
"name_last" => ""
"dob" => null
"mobile" => "9946617986"
"limit_expiry" => "{"PB":"202212"}"
"address" => "PANCHKULA PANCHKULA HARYANA"
"city" => "PANCHKULA"
"pincode" => "134117"
"state" => "HARYANA"
"two_factor" => 0
"status" => 0
"otp_status" => 2
"paytm_key" => "h00097ea42dc-a6ed-42bc-b2cf-3b6a876f878b"
"ab_kyc" => 0
]
Now I did this
$remitterDetails = $this->remitter;
$remitterDetails->beneficiaries = $remitterDetails->beneficiaries->take(50)->map(function ($beneficiary) {
return [
'id' => $beneficiary->bene_id,
'name' => $beneficiary->name,
'account' => $beneficiary->account,
'ifsc' => $beneficiary->ifsc,
'bank' => $beneficiary->bank,
'verificationDt' => $beneficiary->last_success_dt,
];
});
Now the confusing part is this mapping changed my $this->remitter too
after dumping $this->remitter again i get this
App\Models\IpayAppBc\Remitter {#1960
#connection: "ipay_app_bc__write"
+table: "remitter"
#attributes: array:16 [
"id" => 15016478
"name" => "ASKHSYA"
"name_last" => ""
"dob" => null
"mobile" => "1234567"
"limit_expiry" => "{"PB":"202212"}"
"address" => "PANCHKULA PANCHKULA HARYANA"
"city" => "PANCHKULA"
"pincode" => "134117"
"state" => "HARYANA"
"two_factor" => 0
"status" => 0
"otp_status" => 2
"paytm_key" => "h00097ea42dc-a6ed-42bc-b2cf-3b6a876f878b"
"ab_kyc" => 0
"beneficiaries" => Illuminate\Support\Collection {#1956
#items: array:2 [
0 => array:6 [
"id" => "898ddaeff5a54bf2f05a3ce00f5cd13a"
"name" => "DUMMY"
"account" => "37893810436"
"ifsc" => "SBIN0000001"
"bank" => "STATE BANK OF INDIA"
"verificationDt" => "2022-12-01 16:58:57"
]
1 => array:6 [
"id" => "c647579b5881ffc39cb2ffb1fc600634"
"name" => "MRAN DW"
"account" => "3789381043"
"ifsc" => "SBIN0000001"
"bank" => "STATE BANK OF INDIA"
"verificationDt" => null
]
]
#escapeWhenCastingToString: false
}
I am just assuming why this happened is there any referencing happening because as per my knowledge this only happens when I use referencing to any variable ?
any information on this please
Thanks

Laravel LoadSum() base on relation return also model data, how can I prevent it?

I've faced an issue. When I tried to loadSum results based on relations, it also return the model data also. How can I archive only specific data? This is my query.
auth()->user()->loadSum('results', 'total_questions')->loadSum('results', 'correct_answered')
what I get here.
[
"id" => 1
"first_name" => "MH"
"last_name" => "Raihan"
"email" => "me#email.com"
"email_verified_at" => "2022-12-15T11:10:20.000000Z"
"photo_path" => null
"gender" => "male"
"birthday" => "2033-08-25T00:00:00.000000Z"
"country" => "Tajikistan"
"state" => "Alaska"
"city" => "Doylefurt"
"phone" => "908.544.1746"
"address" => """
919 Johns Branch Apt. 486
Batztown, MN 84553-3233
"""
"postcode" => "80799"
"active" => true
"deleted_at" => null
"created_at" => "2022-12-15T11:10:20.000000Z"
"updated_at" => "2022-12-18T14:46:00.000000Z"
"results_sum_total_questions" => "156"
"results_sum_correct_answered" => "35"
]
What I want to expect
[
"results_sum_total_questions" => "156"
"results_sum_correct_answered" => "35"
]
thank you
collect(auth()->user()->loadSum('results', 'total_questions'))
->map(fn($result) => ["results_sum_total_questions" => $result->results_sum_total_questions]);
I've tried to filter data, but it does not work this way.
I do not find on clue yet to solve the issue.
use only() with it
$sums = auth()->user()
->loadSum('results', 'total_questions')
->loadSum('results', 'correct_answered')
->only(['results_sum_total_questions', 'results_sum_correct_answered']);
$data = $sums->toArray();

I tried to import a 3GB system log file through elk but getting errors

files started importing like this..through logstash..kindly suggest me how to remove the errors i use kv filter only in my conf program
{
"authserver" => "a_India RADIUS",
"proto" => "6",
"devname" => "FW_1",
"10:56:12\tdate" => "2020-06-22\tlocal7\tnotice\t\ttime=10:56:11",
"host" => "kali",
"dstintf" => "wan1",
"path" => "/root/Cybrotech-/log00",
"subtype" => "webfilter",
"srcintf" => "ssl.root",
"method" => "domain",
"eventtype" => "ftgd_allow",
"hostname" => "webmail.accessarellc.net",
"cat" => "33",
"srcintfrole" => "undefined",
"dstip" => "20.73.98.154",
"type" => "utm",
"sessionid" => "677535",
"dstintfrole" => "wan",
"srcport" => "6095",
"url" => "/",
"profile" => "monitor-all",
"srcip" => "10.212.134.190",
"logid" => "07013312",
"policyid" => "17",
"eventtime" => "12803571",
"direction" => "outgoing",
"level" => "notice",
"#version" => "1",
"reqtype" => "direct",
"catdesc" => ""Health",
"action" => "passthrough",
"vd" => "root",
"dstport" => "443",
"service" => "HTTPS",
"#timestamp" => 2020-07-13T09:10:47.811Z,
"sentbyte" => "192",
"devid" => "FG0TK19907000",
"group" => "SSLVPN_Group",
"msg" => "URL belongs to an allowed category in policy",
"user" => "\ASINGH",
"rcvdbyte" => "0"
}
after some time i got this error on screen..
"_type"=>"doc", "_id"=>"LzhxR3MBoH6QvDEw21Sy", "status"=>400, "error"=>{"type"=>"illegal_argument_exception", "reason"=>"Limit of total fields [1000] in index [log00_210270] has been exceeded"}}}}

How to get a particular field value from multidimensional array in laravel

I have a variable $cart which stores the details of product. I want to get a particular field from the cart.
dd($cart) show the following result.
Cart {#437 ▼
+items: array:1 [▼
"airports_334_64" => array:4 [▼
"qty" => 1
"price" => 1600000
"duration" => 0
"item" => array:28 [▼
"id" => 64
"created_at" => "2017-05-29 10:24:22"
"updated_at" => "2017-05-29 10:26:51"
"title" => "Airport ad"
"price" => "120000"
"location" => "Airport T3"
"city" => "Delhi"
"state" => "Delhi"
"rank" => "12"
"landmark" => "abc"
"description" => "<p>new</p>"
"image" => "1496053462.jpg"
"references" => ""
"status" => "Available"
"display_options" => null
"light_option" => null
"airportnumber" => null
"discount" => "1"
"slug" => null
"reference_mail" => "chingkhei91#gmail.com"
"airports_id" => "64"
"area" => "arrival_check_in_hall"
"displayoption" => "backlit_panel"
"dimensions" => "7'10"x3'9""
"optionprice" => "1600000"
"units" => "8"
"ad_code" => ""
"variation_id" => 334
]
]
]
+totalQty: 1
+totalPrice: 1600000
}
I want to get the value of reference_mail i.e Cart->items->item->reference_mail
Use the Collections provided by Laravel:
$items = collect($cart->items);
$reference_mails = $items->map(function($item){
return $item['reference_mail'];
});
This leaves you with an array of all the reference mails. The behaviour of map is described here
If you want to access them then you should iterate over the items like so.
foreach ($cart->items as $items) {
foreach ($items as $item) {
$email = $item['item']['reference_mail'];
}
}
If you need to access it directly based on array index then you could do this.
$email = $cart->items['airports_334_64']['item']['reference_mail'];
If you need the list of reference_mail then you do this in your query rather than going through all this. If you have a collection then you can use pluck method to retrieve the list of emails.

ruby one-liner from two hashes

a = {"rows" => [{"id" => "231-z", "name" => 'jon', "age"=> 27, "state" => 'AL'},
{"id" => "4121-x", "name" => 'ton', "age"=> 37, "state" => 'VA'}
]
}
b = {"rows" => [{"key" => ["xyz","4121-x"], "value" =>{"sum" => 12312, "realage" => 29}},
{"key" => ["xyz","231-z"], "value" =>{"sum" => 1212, "realage" => 33}}
]
}
In hash a, age is incorrect
In hash b, realage is correct. Also in hash b id is the second value in the first array that maps to id of hash a . Those are 4121-x, 231-z correspond to hash a
I want to correct the age in hash a and swap it with the realage of hash b
I can do it in multiple steps, but is it possible to do it in one liner or very short? So finally correct hash a should look like
a = {"rows" => [{"id" => "231-z", "name" => 'jon', "age"=> 33, "state" => 'AL'},
{"id" => "4121-x", "name" => 'ton', "age"=> 29, "state" => 'VA'}
]
}
does this look reasonable?
a['rows'].each_with_index do |ah, i|
(bh = b['rows'].select {|h| h['key'].last == ah['id'] }.first) &&
a['rows'][i] = ah.update('age' => bh['value']['realage'])
end
p a
{
"rows" => [
[0] {
"id" => "231-z",
"name" => "jon",
"age" => 33,
"state" => "AL"
},
[1] {
"id" => "4121-x",
"name" => "ton",
"age" => 29,
"state" => "VA"
}
]
}
Please note it will update a only if corresponding id found in b.
Also, the rows order does not matter, nor matter the rows number, it is only important b to have a row with same id as processed row in a
Here is a Working Demo

Resources