I’m working with Laravel 8 to develop my project which is an Online Forum and for this project I wanted to add some notification feature that can alert the user if someone has answered a question that he had asked on the forum.
So basically, when someone answers a question, this method will run:
public function postAnswer(Question $id) { $validate_data = Validator::make(request()->all(),[ 'answer' => 'required', ])->validated(); $answer = Answer::create([ 'answer' => $validate_data['answer'], 'user_id' => auth()->user()->id, 'question_id' => $id, ]); auth()->user()->notify(new RepliedToThread($id)); // making new notification return back(); }
Then, I created this notification called RepliedToThread.php
:
class RepliedToThread extends Notification { use Queueable; protected $thread; /** * Create a new notification instance. * * @return void */ public function __construct($id) { $this->thread = $id; } /** * Get the notification's delivery channels. * * @param mixed $notifiable * @return array */ public function via($notifiable) { return ['database']; } /** * Get the array representation of the notification. * * @param mixed $notifiable * @return array */ public function toDatabase($notifiable) { return [ 'thread' => $this->thread, 'user' => $notifiable ]; } /** * Get the array representation of the notification. * * @param mixed $notifiable * @return array */ public function toArray($notifiable) { return [ // ]; } }
As you can see I have defined a protected variable called $thread
and it is assigned to the $id
variable which is comming from: auth()->user()->notify(new RepliedToThread($id));
After that, I tried returning them by:
public function toDatabase($notifiable) { return [ 'thread' => $this->thread, 'user' => $notifiable ]; }
And finally, I added this to the blade:
<a href=""> {{$notification->data['thread']['title']}}</strong> </a>
But now I get this error:
ErrorException Undefined index: thread
So I don’t really know what is going wrong here! So if you know how to solve this, please let me know, I would really appreciate any idea or suggestion from you guys…
Here is also my table notifications
, if you would like to look:
And this is also the result of {{ dd($notification) }}
on blade:
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Basically, your error caused by there’s no ‘thread’ on the other notifications database. how can you prevent that? just add maybe a type to tell the difference notification view, and you need to tell every other notification with a type name for maybe other hasPost, createSome or other.
public function toDatabase($notifiable) { return [ 'thread' => $this->thread, 'user' => $notifiable, 'type' => 'RepliedThread' ]; }
and then in your notification blade you just use if statement
to check what type is that
@forelse(auth()->user()->unreadNotifications as $notification) @if($notification->data['type'] == "RepliedThread") <a href=""> {{$notification->data['thread']['title']}}</strong> </a> @endif @empty no notification @endforelse
And for the real-time notification you need to use WebSockets Server, there’s a 3rd party for that and its called Pusher or if you want to use your own WebSocket server, Laravel has Laravel-Websockets
. If you dig deeper into that you will find Laravel-Echo
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0