Carosobin's avatar

Call to a member function notify() on null

i am creating A notifcation for user that created post and was awarded a point , so when a user create a post and get a point send notification but im getting error

<?php

namespace App\Http\Livewire;

use App\Gamify\Points\PostCreated;
use App\Models\Category;
use App\Models\Post;
use App\Models\Tag;
use App\Models\User;
use App\Notifications\PointAdd;
use Illuminate\Support\Str;
use Livewire\Component;
use Illuminate\Http\Response;
use Livewire\WithFileUploads;

class PostCreate extends Component
{
    use WithFileUploads;
    public $post;
    public $user;
    public $body;
    public $slug;
    public $photo;
    public $title;
    public $category = 1;
    public $points = 10;
    public $energy = 1;

    public function increment()
    {
        $this->points++;
    }

    protected $rules = [
        'category' => 'required|integer|exists:categories,id',
        'title' => 'required|min:4',
        'body' => 'required|min:4',
    ];

    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),
            ]);
                $user = auth()->user();
                $points = $user->givePoint(new PostCreated($post));
                $this->user->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);
    }




    public function upload()
    {
        $this->validate();


    }
    public function render()
    {
        return view('livewire.post-create', [
            'categories' => Category::all(),
        ]);
    }
}
0 likes
9 replies
webrobert's avatar
Level 51

@carosobin

// this user??
$this->user->notify(new PointAdd($points));

// but two line before you used $user
// do you mean…

$user->notify(new PointAdd($points));

Carosobin's avatar

@webrobert after trying this i got this error

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

$user->notify(new PointAdd($points));
Carosobin's avatar

@Snapey here kindly assist

<?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;

    /**
     * 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,

        ];
    }
}
Snapey's avatar

@Carosobin you need to add

public $user;

at the top of your notification if you want it to hold data

Carosobin's avatar

i noticed the email notification is working probably but the database notification 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 $user;

    /**
     * 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,

        ];
    }
}
Snapey's avatar

you created the database table, but it's empty?

Please or to participate in this conversation.