Reputation: 7128
I need to get logged user notifications by scope but it returns App\User::notifications must return a relationship instance.
layout
@auth
@if(auth::user()->notifications > 0)
<div class="alert alert-success alert-dismissible">
<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
<ul>
@foreach (auth::user()->notifications as $notification)
<li>{{$notification->subject}}</li>
@endforeach
</ul>
<strong>Success!</strong> Indicates a successful or positive action.
</div>
@endif
@endauth
User model
public function notifications() {
// return $this->hasMany(ProjectBroadcastApplicant::class);
$notifications = ProjectBroadcastApplicant::where('user_id', $this->id)->get();
return $notifications;
}
ProjectBroadcastApplicant model
public function user() {
return $this->belongsTo(User::class);
}
Where did I make mistake?!
Upvotes: 0
Views: 135
Reputation: 7128
I've changed my user model to this
public function notifications() {
return ProjectBroadcastApplicant::where('user_id', $this->id)->get();
}
and my blade from @if(auth::user()->notifications > 0)
to @if(auth::user()->notifications() > 0)
now it's working.
Upvotes: 0
Reputation: 412
Update your user Model:
public function notifications() {
return $this->hasMany(ProjectBroadcastApplicant::class);
}
If you want to filter the notification for the user then you can add condition with the relationship definition.
Upvotes: 1