Ja22
Ja22

Reputation: 1712

filter notifications from 'notifications' table 'data' field - Laravel

This is the default laravel notifications data field

{
  "type":"Update Appointment",
  "appointment_id":"379",
  "date":null,
  "updated_by":"Mahir",
  "status":"2"
}

In controller i want to get all notifications with status = 2 and mark as read

Laravel 5.3 doc shows

$user = App\User::find(1);

foreach ($user->unreadNotifications as $notification) {
    $notification->markAsRead();
}

How do i modify this to get all notifications with status = 2

Update : looking for something like this

$noti = $user->unreadNotifications->where('data->status',"2");

Note : my database doesn't support json data type.

Upvotes: 6

Views: 2935

Answers (3)

Nadeem Ahmed
Nadeem Ahmed

Reputation: 36

$user = $user->unreadNotifications->where('data.complaint_id',1);

Its work fine for me, I hope it's helpful for you.

Upvotes: 2

Hamed Yarandi
Hamed Yarandi

Reputation: 1211

According to laravel 5.6: JSON Where Clauses

and work with MySQL 5.7

I hope this answer help you

Upvotes: 5

Yuriy Piskunov
Yuriy Piskunov

Reputation: 1224

I think, you can create your own channel, (see also Illuminate\Notifications\Channels\DatabaseChannel), where you can override send() method, for saving your notifications. Before that, you can write your migration to update "notifications" table to add your custom filtering field. See: https://laravel.com/docs/5.5/notifications#custom-channels

Sorry for my English. Hope, you'll understand.

Upvotes: 1

Related Questions