Laravel 11 Hướng Dẫn Sử Dụng Notification DataTable
Hướng dẫn này sẽ giúp bạn tạo một bảng (DataTable) để hiển thị các thông báo (notifications) trong ứng dụng Laravel 11 của bạn. Bạn sẽ học cách tạo và quản lý thông báo, cũng như hiển thị chúng một cách hiệu quả bằng DataTables, một thư viện JavaScript mạnh mẽ cho việc hiển thị dữ liệu trong bảng.
Trong file controller : app/Http/Controllers/NotificationController.php
<?php
namespace App\Http\Controllers;
use App\Models\User;
use App\Notifications\NewPostNotification;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Notification;
class NotificationController extends Controller
{
public function index(Request $request){
$users = User::cursor();
foreach ($users as $user) {
Notification::send($user ,new NewPostNotification('hehe'));
}
dd('đã gửi notification thành công');
}
public function readnotification(Request $request){
$user = Auth::user();
$unreadCount = $user->unreadNotifications()->count();
return view('notification.unread');
}
public function markAsRead(Request $request, $id)
{
$notification = auth()->user()->unreadNotifications->find($id);
$notification->markAsRead();
return back()->with('success', 'Added Mark as read.');
}
public function markAllAsRead(Request $request)
{
$notifications = auth()->user()->unreadNotifications;
foreach ($notifications as $notification){
$notification->markAsRead();
}
return back()->with('success', 'Added Mark as read.');
}
}
Trong file unread :
@extends('layouts.app')
@push('css')
@endpush
@section('content')
@session('success')
<div class="alert alert-success" role="alert">
{{ $value }}
</div>
@endsession
Số lương : {{ auth()->user()->unreadNotifications->count() }}
@foreach(auth()->user()->unreadNotifications as $notification)
<h3>{{ $notification->type}}</h3>
<h3> [{{ $notification->created_at }}] {{ $notification->data['title'] }}</h3>
<div class="alert alert-success alert-dismissible fade show">
<span><i class="fa fa-circle-check"></i> </span>
{{-- <a href="{{ route('notifications.mark.as.read', $notification->id) }}" class="close" data-dismiss="alert" aria-label="Close">--}}
{{-- <span aria-hidden="true"><strong><i class="fa fa-book-open"></i> Mark as Read</strong></span>--}}
{{-- </a>--}}
</div>
@endforeach
<h3>hehehhe</h3>
@endsection
@push('js')
@endpush