Support the ongoing development of Laravel.io →
Eloquent Blade
Last updated 2 years ago.
0

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

Last updated 2 years ago.
0

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.

Last updated 2 years ago.
0

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();
    }

}
Last updated 2 years ago.
0

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?

Last updated 2 years ago.
0

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

Last updated 2 years ago.
0

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

Last updated 2 years ago.
0

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

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.