Viewed   109 times

I want to have some default data accessible in all views in my Laravel 5 application.

I have tried to search for it but only find results for Laravel 4. I have read the documentation 'Sharing Data With All Views' here but I can't understand what to do. Where should the following code be placed?

View::share('data', [1, 2, 3]);

Thanks for your help.



This target can achieve through different method,

1. Using BaseController

The way I like to set things up, I make a BaseController class that extends Laravel’s own Controller, and set up various global things there. All other controllers then extend from BaseController rather than Laravel’s Controller.

class BaseController extends Controller
  public function __construct()
    //its just a dummy data object.
    $user = User::all();

    // Sharing is caring
    View::share('user', $user);

2. Using Filter

If you know for a fact that you want something set up for views on every request throughout the entire application, you can also do it via a filter that runs before the request — this is how I deal with the User object in Laravel.

  // Set up global user object for views
  View::share('user', User::all());


You can define your own filter

Route::filter('user-filter', function() {
    View::share('user', User::all());

and call it through simple filter calling.

Update According to Version 5.*

3. Using Middleware

Using the View::share with middleware

Route::group(['middleware' => 'SomeMiddleware'], function(){
  // routes

class SomeMiddleware {
  public function handle($request)
    View::share('user', auth()->user());

4. Using View Composer

View Composer also help to bind specific data to view in different ways. You can directly bind variable to specific view or to all views. For Example you can create your own directory to store your view composer file according to requirement. and these view composer file through Service provide interact with view.

View composer method can use different way, First example can look alike:

You could create an AppHttpViewComposers directory.

Service Provider

namespace AppProviders;
use IlluminateSupportServiceProvider;
class ViewComposerServiceProvider extends ServiceProvider {
    public function boot() {

After that, add this provider to config/app.php under "providers" section.


namespace AppHttpViewComposers;

use IlluminateContractsViewView;

class TestViewComposer {

    public function compose(View $view) {
        $view->with('ViewComposerTestVariable', "Calling with View Composer Provider");


Here you are... {{$ViewComposerTestVariable}}

This method could help for only specific View. But if you want trigger ViewComposer to all views, we have to apply this single change to ServiceProvider.

namespace AppProviders;
use IlluminateSupportServiceProvider;
class ViewComposerServiceProvider extends ServiceProvider {
    public function boot() {


Laravel Documentation

For Further Clarification Laracast Episode

If still something unclear from my side, let me know.

Monday, November 14, 2022

Here is how I solved this.

  1. In .env, use the 'stack channel' for logging (i.e. 'LOG_CHANNEL=stack')
  2. Add a 'tap' to a driver in configlogging.php

    'single' => [
        'driver' => 'single',
        'tap' => [AppLoggingCustomizeFormatter::class],
        'path' => storage_path('logs/laravel.log'),
        'level' => 'debug',
  3. Create AppLoggingCustomizeFormatter:

    namespace AppLogging;
    class CustomizeFormatter
        public function __invoke($logger)
            foreach ($logger->getHandlers() as $handler) {
                $handler->pushProcessor(function ($record) {
                    $record['extra']['ip'] = Request::getClientIp();
                    $record['extra']['path'] = Request::path();
                    return $record;
Saturday, December 24, 2022

Recommended approach if you put here only methods (not classes):

  1. Create file anywhere you want
  2. In composer.json make sure you add this file to files key inside autoload like this:

    "autoload": {
        // here other autoload things
        "files": ["app/Helpers/AnythingHelper.php"]
  3. Run composerdump-autoload`

For classes obviously you should use standard PSR-4 autoloading

Sunday, October 23, 2022

The "illuminate/html" component is no more supported by Laravel and is not compatible with the 5.2 version.

You can replace it with laravelcollective/html

You have to:

  • remove the reference of "illuminate/html": "^5.0@dev", from composer.json

  • add "laravelcollective/html": "5.1.*" (or the version you want)

  • run composer update to update the dependecies (this will remove your "illuminate/html" component and install the "laravelcollective/html" component

Now you have to replace your HTML service provider with:

 'providers' => [

and the facades:

  'aliases' => [
      'Form' => CollectiveHtmlFormFacade::class,
      'Html' => CollectiveHtmlHtmlFacade::class,
Wednesday, December 21, 2022

The simple answer is that you don't do this. Password fields should rarely be pre-filled and so because it's not a common occurrence Laravel doesn't support it.

However, if you really want to do this you can use Form::input():

Form::input('password', 'name', 'value')
Thursday, August 11, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :