Paritosh Mishra
Paritosh Mishra

Reputation: 3

what is view and how to view in laravel 8 project

How to add data in array global in appServiceProvider of Laravel which could be used globally for select option field in blade.php

Upvotes: 0

Views: 49

Answers (2)

Manmeet Singh Raina
Manmeet Singh Raina

Reputation: 39

You can use view()->share in boot function of AppServiceProvider.php Class under Providers to make data accessible globally like

   public function boot()

{


 $departments =[

            'IT'=> 'Information Technology Department',

            'BIL'=> 'Billing Department',

            'ACC'=> 'Accounts Department',
            
        ];

    view()->share('departments',$departments);
    

}

and you can use this data stored in $departmentrespectivly in blade.php

<select>
     <option value="">Choose One</option>
     @foreach($departments as $key=> $value)
     <option value="{{ $key }}">{{ $value }}</option>
     @endforeach
   </select>

Upvotes: 0

ml59
ml59

Reputation: 1641

You can use View::share to share data within all views. Here is an example from the doc

<?php

namespace App\Providers;

use Illuminate\Support\Facades\View;

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

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        View::share('key', 'value');
    }
}

Upvotes: 1

Related Questions