you are asking something very complex to reply
you need to grab all the comments order by the field 'created_at' in descending order, also you need to track which was the last comment viewed by the user, to filter 'created_at' field on your query
Sorry for bad English, I just corrected the question to make it more clear.
The notifications are available, I just need to know how to get it without repeating myself in every Controller.
well, use a model named Notification
, with a static method called getLatest
that you call in each controller that needs to execute the query
class Notification extends Eloquent {
protected $table = 'notifications';
public static function getLatest() {
return static::where( 'created_at', '>', User::getLastViewedCommentTimestamp() )
->orderBy( 'created_at', 'DESC' )
->get();
}
}
Yes, this is the point. I just did not whant to repeat it in each controller. It would be about 50 times. Can I load this Model from a blade template?
use View::share
, something like this
View::share( 'notifications', Notification::getLatest() );
with this approach, the variable notifications
will be available in all views in your application
Sounds good for me. So I can use it in the __construct() functions of my Controllers.
Another way I just found to get the number of the notifications:
class NotificationController extends BaseController {
public static function getCount() {
$notifications = Notification::where('users_id', Auth::user()->id)
->where('unseen', 1)
->count();
return $notifications;
}
}
So in the view I am able to use {{ NotificationController::getCount() }}
Thanks for your help
I would suggest that you use view composer. And you could attach it to the key of the notifications so for each time the notification view is loaded your query will be easierxecuted
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community