Carosobin's avatar

Property [id] does not exist on this collection instance.

so i was working on sending user a notifcation on point given after they succefully created a post so the email notification is working but database notifcation is not working

<?php

namespace App\Notifications;

use App\Models\Post;
use App\Models\User;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;

class PointAdd extends Notification
{
    use Queueable;
    public $users;

    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct(User $user)
    {
        $this->user = $user;
    }

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return ['mail', 'database'];
    }

    /**
     * Get the mail representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return \Illuminate\Notifications\Messages\MailMessage
     */
    public function toMail($notifiable)
    {
        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.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function toArray($notifiable)
    {
        return [
            'user_id' => $this->user->id,
            'user_name' => $this->user->name,
            'user_avatar' => $this->user->avatar,
            // 'user_points' => $this->user->reputation,
            // 'post_id' => $this->user->post->id,

        ];
    }
}

post create component

  public function createPost()
    {
        if (auth()->check()) {
            $this->validate();
            $random =  str_pad(mt_rand(1,999999),6,'0',STR_PAD_LEFT);
            $post = Post::create([
                'user_id' => auth()->user()->id,
                'title' => $this->title,
                'category_id' => $this->category,
                'body' => $this->body,
                'post_number' => $random,
                'slug' => Str::slug($this->title),
            ]);
                $users = auth()->user();
                $points = $users->givePoint(new PostCreated($post));
                $users->notify(new PointAdd($points));

                $image = $this->photo->storeAs('posts', str::random(30));
                $post->image = $image;
                $post->save();
                session()->flash("message", "Featured image successfully uploaded");

                preg_match_all('/(?<=#)(\w+)/mi', $this->body, $matchedTags, PREG_SET_ORDER, 0);
                foreach ($matchedTags as $matchedTag) {
                    if (!$tag = Tag::where('name', $matchedTag[0])->first()) {
                        $tag = tag::create(['name' => $matchedTag[0]]);
                    }
                    $post->tags()->attach($tag->id);
                    $tag->addEnergy(1);
                }

                preg_match_all('/(?<=@)(\w+)/mi', $this->body, $matchedMentions, PREG_SET_ORDER, 0);
                foreach ($matchedMentions as $matchedMention) {
                    optional(User::where('username', $matchedMention[0])->first(), function ($user) {
                        // $user->notify(new MentionsNotify($user));
                    });
                 }


            // $users = auth()->user();
            // $users->increment('points', 10);

            session()->flash('success_message', 'Post was added successfully!');

            $this->reset();

            return redirect()->route('post.index');
        }

        abort(Response::HTTP_FORBIDDEN);
    }
0 likes
2 replies
Snapey's avatar
Snapey
Best Answer
Level 122

public $users. ????

Please or to participate in this conversation.