0

So i want to retrieve data from Notifications Table in laravel.

but what i am trying to do is showing notifications on public it means without an auth so it would show in here

notification example

if i do this it throws an error as undefined variable

@foreach( $notifications as $notif)
       lob {{ $notif->type }}
@endforeach

and then i tried to count the unreadNotifications but it throws an error of

Trying to get property 'unreadNotifications' of non-object

what should i do in here? because after i search about this, the solution always use auth()->user()->notifications() meanwhile what i am trying to do is showing notification in home.blade.php without any auth

Regards.

1 Answer 1

1

It is failing because of the reason that there isn't any logged in user.

auth()->user() grabs the logged in user if there is any, otherwise it returns null.

What you have to do here is to get the user for which you want the notifications and then make call to notifications relation on it. For example: for grabbing notifications for user id 1, it would be \App\User::find(1)->notifications

Sign up to request clarification or add additional context in comments.

1 Comment

yes thankyou, to completed this. i put on my controller to get the data and retrieve it with compact like usual $notifications = \App\User::find(1)->notifications;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.