Can you give more details so we can help?
Like what kind of notifications and how is it stored and how do you want it to be deleted?
How to delete database notifications by row in laravel
Can you give more details so we can help?
Like what kind of notifications and how is it stored and how do you want it to be deleted?
Notification means notification when other user like your posts you got a notification that someone has liked your posts and this value store in notification table so if a user want to delete that notification then delete that but I don't know how Please visit to know more https://laravel.com/docs/5.8/notifications
Well like for any other model, you have the $notification->delete() method.
you can access a collection of notifications for a given user by doing $user->notifications()
From this point, you could narrow to the notification you want to delete and proceed with that.
$user->notifications()
->where('id', $notificationId) // and/or ->where('type', $notificationType)
->get()
->first()
->delete();
thanks
I am new to Laravel. I suspect that there are better ways to delete a notification, but here's what I was able to put together until I find a better way. The route is an attempt to use REST syntax, but I was only able to pass the notification ID by using a hidden input. I welcome any input on how to improve this.
Route:
Route::post('/notifications/delete/{id}', [UserNotificationsController::class, 'destroy'])->middleware('auth');
Method in UserNotificationsController:
public function destroy(Request $request)
{
$id = $request->input('id');
auth()->user()->notifications()->where('id', $id)->delete();
return redirect('/notifications')
->with('message', 'Notification deleted.');
}
View:
<div class="container pt80 pb200">
<div class="row justify-content-center">
<div class="col-sm-12">
@if (session('message'))
<div class="alert alert-success">
{{ session('message') }}
</div>
@endif
<h2 class="pb20">{{ __('All Notifications') }}</h2>
<table class="table" id="notification-table">
<thead>
<tr>
<th>Date & time</th>
<th>Notification</th>
<th style="text-align:center;">Action</th>
</tr>
</thead>
<tbody>
@foreach ($notifications as $notification)
<tr>
<td>
{{ $notification->created_at }}
</td>
<td>
@if ($notification->type === 'App\Notifications\ClientApplicationReceived')
We received the Client application from you on.
@endif
@if ($notification->type === 'App\Notifications\BrokerApplicationReceived')
We received the Broker application from you on.
@endif
</td>
<td style="text-align:center;">
<form action="/notifications/delete/{{ $notification->id }}" method="POST">
@csrf
<input type="hidden" name="id" value="{{ $notification->id }}">
<input type="image" src="/assets/images/trash_can_icon_red.png" style="height:20px;">
</form>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
</div>
You do not need a hidden input since you're parsing 'id' in your query strings.
Your controller should be Public function destroy($id){ $user = user()->notifications->where('id',$id)->first(); If(!empty($user)){ $user->delete(); } return back(); }
Please or to participate in this conversation.