Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 574
Ja22 Avatar asked Aug 29 '17 08:08

Ja22


2 Answers

According to laravel 5.6: JSON Where Clauses

and work with MySQL 5.7

I hope this answer help you

like image 176
Hamed Yarandi Avatar answered Oct 28 '22 07:10

Hamed Yarandi


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.

like image 1
Yuriy Piskunov Avatar answered Oct 28 '22 08:10

Yuriy Piskunov