Table of Contents
Overview:
Laravel is a powerful PHP framework that allows developers to quickly and easily create web applications. One of the most important features of the framework is the ability to easily share data between different parts of the application.
This can be accomplished by using Service Providers, which are classes that can be used to bootstrap various parts of the application.
Imagine you want to show some “global” data in the Blade, for example, the number of new users this week on the top navigation.
You may not be sure what Controllers the data would come from, but you can perform that action in the Service Provider. In this article, we will show you three ways to share data using Service Providers in Laravel.
View Share method:
The first option is to use the View Share method. This is done by using the view()->share() method in the boot() method of the AppServiceProvider. The view()->share() method allows you to share a variable with all views, not just specific layouts or sidebars.
Here is an example of how to share the newUsersThisWeekCount variable:
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
view()->share('newUsersThisWeekCount',
User::where('created_at', '>', now()->subDays(7))->count());
}
}
View Composer Callback method:
The second option is to use the View Composer Callback method. This allows you to share data only with specific views. The view()->composer() method is used to specify the view(s) you want to share the data with.
Here is an example of how to share the newUsersThisWeekCount variable with the “layouts.app” view:
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
view()->composer('layouts.app', function ($view) {
$view->with('newUsersThisWeekCount',
User::where('created_at', '>', now()->subDays(7))->count());
});
}
}
You can also provide several views by passing an array of view names to the view()->composer() method.
Additionally, you can use the asterisk (*) to provide the same data to all views, but be careful, as this can cause performance issues if the data is complex and will be loaded for every view separately.
View Composer Class method:
The final option is to use the View Composer Class method. This allows to keep your Service Provider clean and organized by separating the logic into a separate class.
You can create this class manually, and it’s your choice where to put it.
Here is an example of how to create a NewUsersComposer class:
namespace App\View\Composers;
use Illuminate\View\View;
class NewUsersComposer
{
public function compose(View $view)
{
$newUsersThisWeekCount = your_complicated_logic();
$view->with('newUsersThisWeekCount', $newUsersThisWeekCount);
}
}
Then, in the Service Provider, you would register it like this:
use App\View\Composers\NewUsersComposer;
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
view()->composer('layouts.app', NewUsersComposer::class);
}
}
Conclusion:
In conclusion, Service Providers in Laravel provide an easy way to share data between different parts of the application. Whether you want to share data with all views, specific views, or use a separate class to organize your code, Service Providers make it easy to accomplish.
By using the View Share, View Composer Callback, and View Composer Class methods, you can keep your code organized and ensure that the data is shared in the most efficient manner possible.