2

I know this is a bit silly and probably been asked many times but for this one is unique. I'm just asking just for the sake of learning. I have these arrays inside home():

public function home()
    {
        $menus = [ '视频', '直播', '路亚', '海钓', '渔获' ];
        $submenus1 = [ '视频', '直播', '路亚', ];

        return view('/layout', [
            'menus' => $menus,
            'submenus1' => $submenus1,
        ]);
    }

So it's like Nav items. And these items, I want them to be available in all views. Help would be much appreciated. Please respect. Thank you.

DaOneRom
  • 284
  • 4
  • 18
  • 1
    may be duplicate question read this https://stackoverflow.com/questions/28608527/how-to-pass-data-to-all-views-in-laravel-5 – Jignesh Joisar Dec 13 '18 at 06:26
  • 1
    Hi @JigneshJoisar. I've already read about that and it's still giving me a vague answer. I know it may seemed that this is a duplicate question but I'm new to laravel and it's really hard to understand some of these things. Thank you. – DaOneRom Dec 13 '18 at 06:54

1 Answers1

1

You can pass any data with View::share() method in App/Providers/AppServiceProvider.php file. Please check following codes;

<?php

namespace App\Providers;

use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        $menus = [ '视频', '直播', '路亚', '海钓', '渔获' ];
        $submenus1 = [ '视频', '直播', '路亚', ];

        View::share('menus', $menus);
        View::share('submenus1', $submenus1);
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        $this->app->bind('path.public', function() {
            return base_path('public_html');
        });
    }
}

After than you can use $menus and $submenus1 variables anywhere

FGDeveloper
  • 1,030
  • 9
  • 23