Last Updated:

Transfer parameters to all packs in Laravel

Hello. In this article, we'll look at how to pass a value to all the packs in Laravel. For example, we have a table with the settings of our site settings. And this table stores the name of our site site_name. Our task is to convey the value of site_name to all the packs of our site.


And so I have Laravel 5.5.3 installed

all packs in Laravel 1

Create a migration to Laravel.

Let's generate a CreateSettingsTable migration that will create the settings table.

To do this, type the following command in the console:

    php artisan make:migration CreateSettingsTable --create=settings

all packs in Laravel 2

Now let's open this migration and add the following code to the up method:

Schema::create('settings', function (Blueprint $table) {






Let's start the migration:

    php artisan migrate

all packs in Laravel 3

Three tables will be created: users, password_resets (these tables do not interest us) and our settings table with id, key, value, crated_at, updated_at fields.

Creating a model and controller in Laravel

Now let's create a Setting model to work with the settings table. To do this, run the command in the console:

    php artisan make:model Setting -c

Note the –c parameter, which means that a SettingController will also be created.

all packs in Laravel 4

You can not touch the model, but we will add several methods to the SettingController controller:

public function index1()


    return view('index1');


public function index2()


    return view('index2');


public function index3()


    return view('index3');


We added three methods, index1, index2, and index3. Each of these methods calls the appropriate bindweed. Let's create three bindweets: index1.blade.php, index2.blade.php and index3.blade.php. And put the plain text index1 in the index1 binder, index2 in the index2 binder, and index3 index3.

Creating routes

Now let's create three routes. Open the routers/web.php file and add the following code to the end:




Let's check, follow the link <domain>/index1 should open a page with index1. It's cool.

Pass the parameter to all packs

Let's add a string to our settings table: key = site_name, and value = TEST.


Let's create a service provider ViewServiceProvider. To do this, run the following command in the console:

    php artisan make:provider ViewServiceProvider

all packs in Laravel 6

Let's open our newly created service provider and add the following code to it:


namespace App\Providers;

use Illuminate\Support\ServiceProvider;

use App\Setting;

class ViewServiceProvider extends ServiceProvider


    * Bootstrap the application services.
    * @return void
    public function boot()
        view()->share('site_name', Setting::where('key','site_name')->first()->value );

    * Register the application services.
    * @return void
    public function register()

Now let's connect our service provider. To do this, in the config/app file.php in 'providers', add:

'providers' => [

    * Laravel Framework Service Providers...

Well, that's it. Now in the views we can use:

    {{  $site_name  }}

Let's change our packs and add {{ $site_name }} before the text.

Проверяем, переходим <домен>/index1

all packs in Laravel 7

It's cool. Everything works.


You and I have created a migration. Then they ran it. Thus, the settings table was created. Next, with one artisan command, we created a model and a controller. Several methods have been added to the controller. Then we created a service provider, in the boot method, which we added:

    view()->share('site_name', Setting::where('key','site_name')->first()->value );

Thus, the variable site_name was passed to all the packs, in which the value from our table was placed.

But there are situations when you need to pass the parameter to one pack, then we put the following code in the boot method:

view()->composer ('index1', function ($view)


    $view->with('site_name', 'Your name');


In this case, the variable site_name will be passed to the index1.blade.php binder, the value of which is 'Your Name'.