edms2023/app/Http/Controllers/UserNotificationController.php

59 lines
1.5 KiB
PHP

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Notification;
use App\Setting;
use Auth;
use Session;
use DB;
use Carbon;
class UserNotificationController extends Controller
{
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('auth:web');
}
/**
* Show the application dashboard.
*
* @return \Illuminate\Http\Response
*/
public function index(Request $request)
{
$setting = Setting::first();
$limit_val = 10;
if(isset($setting->data_limit_per_page) && $setting->data_limit_per_page > 0)
{
$limit_val = $setting->data_limit_per_page;
}
$notifications = Notification::where('notifiable_id', Auth::user()->id)->orderBy('created_at', 'desc')->paginate($limit_val);
return view('notifications', compact('notifications'));
}
public function headerNotifications(Request $request)
{
$notifications = Notification::where('notifiable_id', Auth::user()->id)->whereNull('read_at')->orderBy('created_at', 'desc')->get();
$count_not = count($notifications);
$request->session()->put('unread_notification_number', $count_not);
return view('includes.header-notifications', compact('notifications', 'count_not'));
}
public function markAsRead()
{
Notification::where('notifiable_id', Auth::user()->id)->whereNull('read_at')->update(['read_at' => now()->format('Y-m-d H:i:s')]);
return back();
}
}