0

I am trying to count the number of elements that are equal to a certain value but im struggling to get to the element that i want to compare, when i run:

{{dd($numberofnotifications)}}

I get the following:

enter image description here

But the value i need to compare is under 'attributes'

so how do i get to the values under attributes?

When i print out each element of the array i get the following format:

{"id":"96a40ebb-a2d1-44d8-9600-e94dd026f152","type":"App\\Notifications\\CommentCreated","notifiable_type":"App\\User","notifiable_id":1,"data":{"comment_id":9,"data":{"name":"John","date":"2020-12-30T08:37:47.000000Z","email":"John@gmail.com","post":2,"body":"test"},"message":"John commented on your post"},"read_at":null,"created_at":"2020-12-30T08:37:47.000000Z","updated_at":"2020-12-30T08:37:47.000000Z"}
es915
  • 137
  • 3
  • 11

4 Answers4

0

As the result is a collection, you can convert them using

$numberofnotifications->toArray();
SEYED BABAK ASHRAFI
  • 4,093
  • 4
  • 22
  • 32
0

you'll need to use foreach to iterate through the collection:

foreach($numberofnotification as $number)
    {
        $number->name;   //name here is the "name" of the attribute you want to access
    }

if you want to store them in an array then:

foreach($numberofnotification as $number)
    {
       $attributes[]=$number->name;   //name here is the "name" of the attribute you want to access
    }

 
0

Did you try this

//consider that $username is the logged in user name

$filtered_count = $numberofnotifications->where('name', $username)->count();

and if you want the record

$filtered = $numberofnotifications->where('name', $username)->all();
Luay AL-Assadi
  • 426
  • 7
  • 16
0

It seems that the items is an array of DatabaseNotifications. So, you can get each element manually by its index:

$numberofnotifications[0]->id;
// or
$numberofnotifications[0]->type

or you can use foreach loop:

foreach ($numberofnotifications as $notification) {
    $notification->id;
}
Alisher Nasrullayev
  • 565
  • 1
  • 6
  • 22
  • Okay so i have managed to figure it out your way, but now how do i count how many are equal to a value. this is what ii have managed to use to get to the name value: $notification->data['data']['name'], and this is how i compare the two: $notification->data['data']['name'] == Auth::user()->name. How would i count how many this returns – es915 Dec 30 '20 at 12:01
  • Then, use where() and count() befor getting your data: $data = Model::query()->where('your_field', $your_value)->count(); – Alisher Nasrullayev Dec 30 '20 at 12:05
  • what like this: transform($notification->data['data']['name']) – es915 Dec 30 '20 at 12:07
  • Sorry, I have confused. I've edited my comment above – Alisher Nasrullayev Dec 30 '20 at 12:09
  • like this: $data = $numberofnotifications->where($notification->data['data']['name'], '==', Auth::user()->name)->count(). This gives me a count of 5 which is the total number of elements which is incorrect. but if i take out the where() and the count it is displaying all the name values that are equal to the current users name which is 4 which is correct – es915 Dec 30 '20 at 12:14
  • What is $numberofnotifications here? It is a collection result of model? You should not use ready collection of data. Just use count instead of get(): $numberofnotifications = Notifications::query()->where('username', Auth::user()->name)->count(). If your Notification model have user_id that relates to users table, then try this: $username = Auth::user()->name; Notification::query()->whereHas('users', function (Builder $query) use ($username) { $query->where('username', $username); })->count(); – Alisher Nasrullayev Dec 30 '20 at 12:25
  • numberofnotifications is just a collection of all the unread notifications of the user that is logged in – es915 Dec 30 '20 at 12:27
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/226601/discussion-between-alisher-nasrullayev-and-es915). – Alisher Nasrullayev Dec 30 '20 at 12:27