0

I am trying to make a notification badge show the amount of unread messages a user has. The following works:

// Controller
public function messages() 
{
    $messages = MessagesController::getMessages();
    $newNotificationNumber = MessagesController::getNumberOfNewMessages();

    return view('pages.messages', compact('messages'), compact('newNotificationNumber'));
}

My app.blade.php file is structured like so:

// html stuff
@include('layouts.navbar')
<main class="py-4 bg-white">
  <div class="container">
    @yield('content')
  </div>
</main>

The navbar shows the number like so:

<span class="badge badge-pill badge-primary">{{ $newNotificationNumber ?? '' }}</span>

If I include compact('newNotificationNumber') in every single controller function my notifications work like I want, but that is tedious and prone to error. Any suggestions?

leiflundberg
  • 334
  • 2
  • 13

2 Answers2

1

You could do create a new controller, let's say AppController, which will extend Laravel's default App\Http\Controllers controller. Inside this new controller, create your constructor with all the data you need, and share them to all the views:

public function __construct(Request $request)
{
    $messages = MessagesController::getMessages();
    $newNotificationNumber = MessagesController::getNumberOfNewMessages();
    View::share('languages', $languages);
    View::share('newNotificationNumber', $newNotificationNumber);
}

After that, you can extend AppController in every other controller where you need your variables:

class YourController extends AppController

All that is left to do now is to extend AppController constructor in YourController:

public function __construct()
{
    parent::__construct();
}

This way, you will have access to $languages and $newNotificationNumber variables in all the views you're using in your YourController.

zlatan
  • 3,346
  • 2
  • 17
  • 35
1

The best way I will recommend you to use Service provider. You can AppServiceProvider.php located in app\Providers or Create a new Service Provider(Which I will highly recommend);

First let's create a new Provider call MasterViewServicerProvider

*php artisan make:provider MasterViewServicerProvider

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\View; /** Let's Import View Facade **/
use App\Model\CAS\Message; /** Let's Import the our Model **/

class MasterViewServicerProvider extends ServiceProvider
{

public function register()
{
    //
}

/**
 * Bootstrap services.
 *
 * @return void
 */
public function boot()
{

/** First we have to check if the table exist in our database, the reason is that if we try to run migration and the table doesn't Message doesn't exist, it will through an error because the provider will render before the migration **/

    if(\Schema::hasTable('messages'))
    {
      $message= Message::all();
      /** message = Message::get(['title','id']); // get specific column instead of all record on the table  **/
      View::share('message', $message);
    }
}

}

Now Let Register our Provider in conf\app.php

'providers' => [
    .......
    App\Providers\MasterViewServicerProvider::class,
],

Now you can call $message variable anywhere in the app. Hopeful this help.

livreson ltc
  • 733
  • 8
  • 22