How can I mark each notification for a user as read, currently I do mark all as read like this:
Route
Route::group(['prefix' => 'notification', 'as' => 'notification.'], function () {
Route::delete('destroy/{userID}}', ['as' => 'destroy', 'uses' => 'NotificationController@destroy']);
});
Controller
public function destroy($userID)
{
$user = User::findOrFail($userID);
$user->unreadNotifications->map(function($n) {
$n->markAsRead();
});
return back();
}
Form
<form method="post" action="notification/destroy/{{ Auth::id() }}">
{{ method_field('DELETE') }}
{{ csrf_field() }}
<button type="submit">Mark as read.</button>
</form>
If you have in your form some thing like this :
<form method="post" action="notification/destroy/{{ Auth::id() }}">
{{ method_field('DELETE') }}
{{ csrf_field() }}
<input type="hidden" name="notification_uuid" value="{{ $notification->id }}">
<button type="submit">Mark as read.</button>
</form>
All you need to do is add a test in the map method like this :
public function destroy($userID, , Request $request)
{
$user = User::findOrFail($userID);
$user->unreadNotifications->map(function($n) use($request) {
if($n->id == $request->get('notification_uuid')){
$n->markAsRead();
}
});
return back();
}
try this :
public function destroy($userID)
{
$user = User::findOrFail($userID);
$user->unreadNotifications->get()->map(function($n) {
$n->markAsRead();
});
return back();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.