Export array globally with the Laravel framework

0

Hey guys!

I'm working with Laravel on a project, and a question arose to me even being (idiot). I have structured all my controllers, models and views, but I have a view called: base.blade.php the same idea of layout.blade.php that comes when Laravel is installed.

In it is the whole basis of the project, and in it I inject my content, or it is in it that is the: @yield('content')

What I have to do in my view is a div that will look like the nav title and description of the alert, and click to go to the alert clicked. And that's where the problem is, as the notifications div is in my layout.blade.php I can not import an array and perform the foreach.

How can I make a return in my controller that is accessible by base.blade.php ? Or would I have to set a $alerts = Alert::all(); in all controllers and return to each view I create?

Print from my nav that receives alert data: link (Accessed only in my alerts view).

Error accessing any other page that uses my root view base.blade.php link

    
asked by anonymous 29.09.2018 / 20:54

1 answer

0

Solved!

I did not have to play in my controller, model or view. I had not made use of provideres and it was with him that I solved it.

In my provider app\Providers\AppServiceProvider

I edited and left it as follows:

<?php

namespace App\Providers;

use App\Alert;
use Illuminate\Support\Facades\View;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        Schema::defaultStringLength(191);

        $alerts = Alert::all();
        View::share('alerts', $alerts);
    }

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

I used my call to the Alert model, and with View::share('alerts', $alerts); I globally export the alert array.

    
01.10.2018 / 15:11