Level 51
// this user??
$this->user->notify(new PointAdd($points));
// but two line before you used $user
// do you mean…
$user->notify(new PointAdd($points));
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(),
]);
}
}
// this user??
$this->user->notify(new PointAdd($points));
// but two line before you used $user
// do you mean…
$user->notify(new PointAdd($points));
Please or to participate in this conversation.