CasperSecurity
<?php
namespace App\Http\Livewire\Header;
use App\Models\User;
use Illuminate\Support\Facades\Auth;
use Livewire\Component;
class NotificationLivewire extends Component
{
public $notifications=[];
public $is_hidden=true;
protected $listeners=['updateNotification'];
public function updateNotification()
{
$this->getNotifications();
}
public function noficiationclick(){
if($this->is_hidden){
$this->is_hidden=false;
}else{
$this->is_hidden=true;
}
}
public function markasread($keyval,$targeturl){
$user = User::find(Auth::user()->id);
if($keyval !=""){
try{
$notification=$user->unreadnotifications[$keyval];
if($notification){
$notification->markAsRead();
$this->getNotifications();
}
}catch (\Exception $e){
$this->getNotifications();
}
if($targeturl !=""){
$this->redirect($targeturl);
}
}
}
public function getNotifications(){
$this->notifications=[];
$user = User::find(Auth::user()->id);
foreach ($user->unreadnotifications as $notification) {
array_push($this->notifications,json_decode($notification,true)) ;
}
// dd($this->notifications);
}
public function render()
{
$this->getNotifications();
return view('livewire.header.notification-livewire');
}
}