View Composers in laravel

When you start working on laravel, you will come into the situation where you require same data in multiple views. ie, sidebars or side widget. so it’s not a good idea to fetch same data again and again for every views. so for this we can use view composer which will help you to get same data multiple views without calling multiple times.

For this we are going to consider AppServiceProvider.php which lies inside Providers folder. There are two functions inside AppServiceProvider.php, one is boot(), another is register(). But here we are going to make use of boot() function. In boot function you can perform any action assuming that framework is fully loaded. Now make use of view helper function as shown below:

<?php
class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {

        view()->composer('layouts.sidemenu',function($view){
            $notificationinfo=new NotificationAlertController();
            $view->with('notificationCount',$notificationinfo->showNotificationCount());
        });
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}

Here we can hook into when any view is loaded. It’s just like a callback. Now let me go to my view which makes use of sidemenu all the time. This sidemenu is called from all other views. In my sidebar I have notification count, so I have decided which ever views call sidebar, it should show notification count.

Now let us explain in brief about the code that we have written inside the boot(). When a layout.sidemenu is loaded in any views, we call a callback function where we can bind anything to that view. Within a callback function, i have created an object for NotificationAlertController.

Then I have called a function with that controller named as showNotificationCount(), from this function we are getting a value of notificationcount, which in turn we are binding that returned value to the view.

Now let enclose function showNotificationCount() of NotificationAlertController too.

<?php
class NotificationAlertController extends Controller
{
 public  function showNotificationCount(){

        $notificationCount=DB::table('current_device_profile_infos')
            ->select('current_device_profile_infos.vin_number', 'battery_level', 'fuel_level', 'mileage', 'id')
            ->where('battery_level', '<', 12.5)
            ->orWhere('fuel_level', '<', 20)
            ->orWhere('mileage', '>', 11000)->count();

        return $notificationCount;
    }

}

Now which ever view you click into, but the notification count is displayed everywhere. This is a short and easy explanation on view composer.

If you have any query, feel free to contact me.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s