php - Laravel - display notification count on navigation bar -
currently im working on notification features in laravel apps. notification doesn't need real time, users refresh page system latest notification count again , display on top of navigation bar. i've achieved implementing count function in base controller other controller extend it. below example of how notification count.
i've 2 table, ap_thread
, ap_thread_comment
. ap_thread has column last_visit_date , ap_thread_comment
has column created_at
. 1 thread can have many comments, query when ap_thread_comment created_date
> ap_thread last_visit_date
, getthe total unread comment. ap_thread last_visit_date
update when thread owner visit thread.
now problem when user comment on thread let's 2 unread comments. when thread owner visit thread again, show 2 unread comments it's because base controller trigger first follow controller update last_visit_date
. can correct count if refresh page again. doing wrong implement notification this? below code
class basecontroller extends controller{ public function __construct() { $new_thread_comment_count = 50; // 50 unread comments example. view::share('new_thread_comment_count', $new_thread_comment_count); } class threadcontroller extends basecontroller{ // update last visit date function }
i assume when setting thread comment count. since calling in constructor of method getting unread count in proceedings.
i suggest instead of using view::share()
use view composer. view composers give lazy evaluation called before view rendered.
you attach view composer in application service provider in register method with;
// '*' attach composer views, if want single view // specify it's name, or can specify array of views. view()->composer('*', function (view $view) { $new_thread_comment_count = 50; $view->with('new_thread_comment_count', $new_thread_comment_count); });
as noted in documentation composers if aren't fan of closures or putting logic service providers can put named class in there;
view()->composer('*', 'app\http\viewcomposers\newthreadcommentcounter');
Comments
Post a Comment