ConflictBoy
ConflictBoy

Reputation: 35

Laravel Database notification - group by post id and type

I cannot figure out one problem with Database notification in Laravel. I wish to show user notifications like Facebook. For example: "User X, Y and 2 other people commented your photo". My database structure looks like this:

+--------------------------------------+------------------------------+-----------------+---------------+-----------------------------------------+---------+---------------------+---------------------+
|                  id                  |             type             | notifiable_type | notifiable_id |                        data             | read_at |     created_at      |     updated_at      |
+--------------------------------------+------------------------------+-----------------+---------------+-----------------------------------------+---------+---------------------+---------------------+
| 6d57d30c-419c-4f22-9aaa-f6bb22354887 | App\Notifications\NewComment | App\Models\User |             1 | {"userId":4,"commentId":42,"albumId":3} |         | 2020-01-29 18:00:10 | 2020-01-29 18:03:15 |
| 7d57d30c-419c-4f22-9aaa-f6bb22354887 | App\Notifications\NewComment | App\Models\User |             1 | {"userId":4,"commentId":42,"albumId":3} |         | 2020-01-29 18:00:15 | 2020-01-29 18:03:15 |
| 8d57d30c-419c-4f22-9aaa-f6bb22354887 | App\Notifications\NewComment | App\Models\User |             1 | {"userId":5,"commentId":42,"albumId":2} |         | 2020-01-29 18:03:15 | 2020-01-29 18:03:15 |
| 9d57d30c-419c-4f22-9aaa-f6bb22354887 | App\Notifications\NewComment | App\Models\User |             1 | {"userId":4,"commentId":42,"albumId":2} |         | 2020-01-29 18:02:15 | 2020-01-29 18:03:15 |
+--------------------------------------+------------------------------+-----------------+---------------+-----------------------------------------+---------+---------------------+---------------------+

I can't figure out how to group by type and albumId (within the data column.) Can you help me please? I still overwriting User model method:

public function getNotificationsAttribute()
{
    $notifications = $this->notifications();
    $notifications->groupBy('type', 'data->albumId')->orderBy('created_at', 'desc')->get();
    return $notifications;
}

Upvotes: 1

Views: 1135

Answers (1)

miken32
miken32

Reputation: 42725

It would be very challenging to do this with the query builder (even more so since this isn't a JSON column) but if you're working with a collection, groupBy() can take a callback function, so it's quite simple to do something like this:

public function getNotificationsAttribute()
{
    return $this
        ->notifications()
        ->get()
        ->groupBy("type"),
        ->groupBy(function($data) {
            return $data["albumId"];
        })
        ->orderBy("created_at", "desc");
}

This assumes that the Eloquent model is configured to cast the column as an array.

protected $casts = ["data" => "array"];

Upvotes: 3

Related Questions