LikedTweetNotification
User Model
use Illuminate\Notifications\Notifiable;
use HasApiTokens, HasFactory, Notifiable;
LikedTweetNotification
class LikedTweetNotification extends Notification
{
use Queueable;
protected $tweet;
protected $user;
/**
* Create a new notification instance.
*/
public function __construct(Tweet $tweet, User $user)
{
$this->tweet = $tweet;
$this->user = $user;
}
/**
* Get the notification's delivery channels.
*
* @return array<int, string>
*/
public function via(object $notifiable): array
{
return ['database'];
}
/**
* Get the mail representation of the notification.
*/
public function toMail(object $notifiable): MailMessage
{
return (new MailMessage)
->line('The introduction to the notification.')
->action('Notification Action', url('/'))
->line('Thank you for using our application!');
}
/**
* Get the array representation of the notification.
*
* @return array<string, mixed>
*/
public function toArray(object $notifiable): array
{
return [
'tweet' => $this->tweet,
'user' => $this->user,
];
}
}
HandleInertiaRequests
return array_merge(parent::share($request), [
'auth.user' => fn () => $request->user()
? $request->user()->only('id', 'name', 'username', 'email', 'profile_photo_path')
: null,
'unreadNotifications' => $request->user()->unreadNotifications->count(),
]);
LikedTweetsController
public function toogle(Tweet $tweet) {
$result = $tweet->likes()->toggle(auth()->id());
if(count($result['attached'])) {
$tweet->user->notify(new LikedTweetNotification($tweet, auth()->user()));
}
return $tweet->load('user')
->loadCount([
'likes',
'likes as liked' => function($q) {
$q->where('user_id', auth()->user()->id);
}
]);
}
UserNotificationsController
class UserNotificationsController extends Controller
{
public function index() {
return Inertia::render('notifications', [
'notifications' => auth()->user()->notifications()->latest()->paginate()
]);
}
}
notifications() burada bana Undefined method 'notifications' hatası veriyor. Ayrıca databasede 3 tane notification olmasına rağmen bana vue tarafında 0 tane olduğunu gösteriyor.
Not: Forumdakı hata
https://prnt.sc/x-4vvh-0bimn
https://prnt.sc/6wyFuAgaTpul