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);
Related
Im my laravel project I have this nested array that needs to be updated (transformed - for the frontend):
array:2 [
0 => array:3 [
"entity_id" => 931
"entity" => "user"
"children" => array:2 [
0 => array:2 [
"entity_id" => 5
"entity" => "location"
]
1 => array:2 [
"entity_id" => 932
"entity" => "user"
]
]
]
1 => array:2 [
"entity_id" => 486
"entity" => "user"
]
]
EXPECTED ARRAY:
array:2 [
0 => array:4 [
"id" => 931
"text" => "Test User 1"
"type" => "user"
"children" => array:2 [
0 => array:3 [
"id" => 5
"text" => "Location 1"
"type" => "location"
]
1 => array:3 [
"id" => 932
"text" => "Test User 2"
"type" => "user"
]
]
]
1 => array:3 [
"id" => 486
"text" => "Test User 3"
"type" => "user"
]
]
I have created a recursive function for this job
public function getNewStructure($tree, &$output) {
foreach($tree as $data) {
$output[] = array(
'id' => $data['entity_id'],
'text' => $data['user'] === 'user' ? User::find($data['entity_id'])->name : Location::find($data['entity_id'])->name,
'type' => $data['entity']
);
$this->getNewStructure($data['children'] ?? [], $output);
}
return $output;
}
but it not returns as expected:
array:4 [
0 => array:3 [
"id" => 931
"text" => "Test User 1"
"type" => "user"
]
0 => array:3 [
"id" => 5
"text" => "Location 1"
"type" => "location"
]
1 => array:3 [
"id" => 932
"text" => "Test User 2"
"type" => "user"
]
1 => array:3 [
"id" => 486
"text" => "Test User 3"
"type" => "user"
]
]
How can I add the children to the $output array in the recursive function ???
I have tried by adding the children key:
$this->getNewStructure($data['children'] ?? [], $output['children']);
as when iterating again it will push the current array in the right place.... but is not working...
I fixed
public function getNewStructure($tree, &$output) {
foreach($tree as $data) {
$element = array(
'id' => $data['entity_id'],
'text' => $data['user'] === 'user' ? User::find($data['entity_id'])->name : Location::find($data['entity_id'])->name,
'type' => $data['entity']
);
if(is_array($data['children']) && count($data['children']) > 0) {
$element['children'] = $this->getNewStructure($data['children'], $element['children']);
}
$output[] = $element;
}
return $output;
}
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
}
When I used
return $request->all();
network preview tab
0 => array:10 [
"id" => 1
"user_id" => 1
"product_id" => 1
"brand_id" => 1
"quantity" => 2
"total" => "90000"
"created_at" => "2021-05-21T07:48:34.000000Z"
"updated_at" => "2021-05-21T07:48:34.000000Z"
"product" => array:10 [
"id" => 1
"name" => "S1 Pro"
"category_id" => 1
"sub_category_id" => 1
"brand_id" => 1
"buy_price" => "40000.00"
"sell_price" => "45000.00"
"description" => "this is a new phone"
"created_at" => "2021-05-20T06:02:28.000000Z"
"updated_at" => "2021-05-20T06:02:28.000000Z"
]
"brand" => array:4 [
"id" => 1
"name" => "Vevo"
"created_at" => "2021-05-20T05:48:01.000000Z"
"updated_at" => "2021-05-20T05:48:01.000000Z"
]
]
1 => array:10 [
"id" => 2
"user_id" => 1
"product_id" => 2
"brand_id" => 2
"quantity" => 1
"total" => "195000"
"created_at" => "2021-05-21T08:49:47.000000Z"
"updated_at" => "2021-05-21T08:49:47.000000Z"
"product" => array:10 [
"id" => 2
"name" => "Note 20 ultra"
"category_id" => 1
"sub_category_id" => 2
"brand_id" => 2
"buy_price" => "180000.00"
"sell_price" => "195000.00"
"description" => "A new phone a new Era"
"created_at" => "2021-05-21T06:39:43.000000Z"
"updated_at" => "2021-05-21T06:39:43.000000Z"
]
"brand" => array:4 [
"id" => 2
"name" => "Samsung"
"created_at" => "2021-05-21T06:37:54.000000Z"
"updated_at" => "2021-05-21T06:37:54.000000Z"
]
]
]
But when Im using foreach loop to access every item it give error of "Trying to get property 'product' of non-object"
method I'm using
public function sellProduct(Request $request){
// dd($request->all());
$cardData = $request->all();
foreach ($cardData as $value) {
return $value->product;
}
}
please help to resolve this issue, basically I'm creating a cart app through request I'm getting all the details and product
Access the product like $value['product'] since it's an array and not an 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');
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.