In my project with Symfony2, I'm using Doctrine createNativeQuery, and I would like to get an associative array.
This is my code
$rsm = new ResultSetMapping;
$rsm->addScalarResult('id', 'id');
$rsm->addScalarResult('name', 'name');
$rsm->addScalarResult('phone_one', 'phoneOne');
$rsm->addScalarResult('phone_two', 'phoneTwo');
I have this result:
array:2 [
0 => array:4 [
"id" => "975"
"name" => "one name"
"phoneOne" => "122345556"
"phoneTwo" => "345566789"
]
1 => array:4 [
0 => array:4 [
"id" => "976"
"name" => "two name"
"phoneOne" => "122345556"
"phoneTwo" => "345566789"
]
]
It's posible this result?
array:2 [
0 => array:4 [
"id" => "975"
"name" => "one name"
"phones" => [
"phoneOne" => "122345556"
"phoneTwo" => "345566789"
]
]
1 => array:4 [
0 => array:4 [
"id" => "976"
"name" => "two name"
"phones" => [
"phoneOne" => "122345556"
"phoneTwo" => "345566789"
]
]
]
Thanks a lot
If that is the result you want, why not create a OneToMany for Users to Phones? I would highly suggest you do that.
Related
I would like to transform a collection of object in laravel, but I can't figure it out how. I want to group the record by the type column, and set the grouped record under an array key value.
public function transformData()
{
$data = collect();
$matches = [ 'race', 'ethnicity'];
$attributes = Attribute::withTrashed()
->whereIn('type', $matches)
->get()
->groupBy('type');
$data->put(
$type, // to be the group key(like race)
[
'values' => $attributes->toArray(), // to be all the value with type race
]
);
dd(collect($data));
}
Here is what I'm getting:
Illuminate\Support\Collection^ {#2368
#items: array:1 [
"values" => array:2 [
"ethnicity" => array:2 [
0 => array:2 [
"id" => 1
"type" => "ethnicity"
]
1 => array:2 [
"id" => 2
"type" => "ethnicity"
]
]
"race" => array:2 [
0 => array:2 [
"id" => 6
"type" => "race"
]
1 => array:2 [
"id" => 7
"type" => "race"
]
]
]
]
#escapeWhenCastingToString: false
}
BUT I would like to return this instead:
Illuminate\Support\Collection^ {#2368
#items: array:1 [
"ethnicity" => array:2 [
"value" => array:2 [
0 => array:2 [
"id" => 1
"type" => "ethnicity"
]
1 => array:2 [
"id" => 2
"type" => "ethnicity"
]
]
]
"race" => array:2 [
"value" =>
0 => array:2 [
"id" => 6
"type" => "race"
]
1 => array:2 [
"id" => 7
"type" => "race"
]
]
]
#escapeWhenCastingToString: false
}
I have an API request like this:
$postInformation =
(new postInformation)->handle(['field' => 'slug', 'value' => $this->slug]);
A dump of this response, besides other things, show some categories that are coming from this API request. And they are on this format where there is a 'categories' key with the top categories:
categories:
^ array:40 [
"id" => 2
...
"categories" => array:2 [
"data" => array:15 [
0 => array:3 [
"id" => 6
"name" => array:1 [
"en" => "General"
]
"on_request" => 0
]
1 => array:3 [
"id" => 14
"name" => array:1 [
"en" => "Tuts"
]
"on_request" => 0
]
2 => array:3 [
"id" => 3
"name" => array:1 [
"en" => "Laravel"
]
"on_request" => 0
]
...
]
...
]
I created a table 'post_top_categories' and a model PostTopCategory and I want to get the top categories from the API response above and store them into the 'post_top_categories' table. But I'm not understanding how to properly achieve this. Do you know how it can be achieved it? Thanks
foreach($yourArray['categories']['data'] as $topCategory)
{
$catId = $topCategory['id'];
$catname = $topCategory['name']['en'];
$catOnRequest = $topCategory['on_request'];
// Do what you want with those values now
}
I have an array(or collection) like this for my notifications. Each notification has an array field for all users who has seen the notification.
array:2 [
0 => array:8 [
"_id" => "604485ddf92e0000050079c6"
"title" => "Notification title"
"message" => "Notification message"
"type" => "success"
"expire_date" => "2021-05-09T19:30:00.000000Z"
"read_by_users" => array:2 [
0 => array:2 [
"user_id" => "601fd68d212200006f001c0b"
"seen_time" => 1615103466
]
1 => array:2 [
"user_id" => "60365402546900008a007c76"
"seen_time" => "1615103466"
]
]
"updated_at" => "2021-03-07T07:50:53.314000Z"
"created_at" => "2021-03-07T07:50:53.314000Z"
]
1 => array:8 [
"_id" => "604497d2f92e0000050079c7"
"title" => "Next Notification title"
"message" => "Next Notification message"
"type" => "success"
"expire_date" => "2021-05-09T19:30:00.000000Z"
"read_by_users" => array:1 [
0 => array:2 [
"user_id" => "601fd68d212200006f001c0b"
"seen_time" => 1615187929
]
]
"updated_at" => "2021-03-07T09:07:30.066000Z"
"created_at" => "2021-03-07T09:07:30.066000Z"
]
]
Now I want to get notifications where has not seen by current user (for example with this user_id 60365402546900008a007c76).
Any solution would be apreciated 🙏.
My project is in Laravel 8 and MongoDB.
If you want the result using array here is a solution
$userId = '60365402546900008a007c76'; $notifications = [];
foreach ($data as $notification) {
$userIds = array_column($notification['read_by_users'], 'user_id');
if (!in_array($userId, $userIds)) {
$notifications [] = $notification;
}
dd($notifications);
I'm transforming my data into an array structure. Now I have the meta which already unserialized and I just need to take a specific value in meta.
Code
$item->meta;
$itm['attrs'] = #unserialize($item->meta);
$serial = #unserialize($item->meta);
$itm['product']['attrs'] = $itm['attrs'];
$itm['product']['serial'] = $serial['serial'];
Result
"product" => array:2 [
"attrs" => array:1 [
"serial" => array:1 [
0 => array:3 [
"id" => 848
"text" => "12345wf"
"trade_in" => 0
]
]
]
"serial" => array:1 [
0 => array:3 [
"id" => 848
"text" => "12345wf"
"trade_in" => 0
]
]
]
Expected Result
"product" => array:2 [
"attrs" => array:1 [
"serial" => array:1 [
0 => array:3 [
"id" => 848
"text" => "12345wf"
"trade_in" => 0
]
]
]
"serial" => "12345wf"
]
I'm not sure how to get the value and pass to the object.
$response['inserted_data'] =
array:3 [
0 => array:16 [
"name" => "Khv"
"emails" => array:1 [
0 => array:1 [
"email" => "demo#yahoo.com"
]
]
]
1 => array:18 [
"name" => "Aesha"
"emails" => array:1 [
0 => array:1 [
"email" => "test2#gmail.com"
]
]
]
How to get email values demo#yahoo.com and test2#gmail.com using pluck - php laravel and then from that emails need to get _id from database and update that records.
Try the below code :
Assuming your data look likes below array i.e $arrays.
$arrays=[
["name" => "Khv","emails" =>
[0 => ["email" => "demo1#yahoo.com"]
]
],
["name" => "asy", "emails" =>
[0 => ["email" => "demo2#yahoo.com"]
]
]
];
$emails=collect();
foreach ($arrays as $array) {
$emails->push($array['emails'][0]['email']);
}
dd($emails);
Note : You can't use pluck on array, it can be only used on collection.
You get the value from the pluck like below way.
User::all()->pluck('email');