i am creating A notifcation for user that created a new post on my site and the user will be awarded point after creating a post, so when a user create a post and get a point send notification but im getting this error
???? Property [id] does not exist on this collection instance.
please how can I resolve this issue kindly assist me
<?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));
$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);
}
}
My notification components
<?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,
];
}
}
My Answer:
To resolve this issue, you need to make sure that you are accessing the correct property on the collection instance. In Laravel, when you retrieve a collection of models, you need to access the properties using the `->` operator.
For example, if you are trying to access the `id` property of a user model in a collection, you should do something like this:
php
$user = $users->first(); // Assuming $users is the collection of users
$userId = $user->id;
Make sure to replace `$users` with your actual collection variable name.
If you are still facing issues, please provide more details or code snippets so that I can assist you further.
Rate this post
5 of 5 based on 5130 votesComments