Export array globally with Laravel framework

Asked

Viewed 31 times

0

Eai galera!

I’m working with Laravel on a project, and I came up with a question 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 which comes when the Laravel is installed.

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

What I have to do in my view is a div that will appear on the Nav the title and description of the alert, and when clicking go to the clicked alert. And that’s where the problem lies, as the notification div is in my layout.blade.php cannot import an array and perform foreach.

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

Print of my Nav receiving the alert data: http://prntscr.com/l08rrm (Accessed only in my alerts view).

Error accessing any other page that uses my root view base.blade.php http://prntscr.com/l08ssz

  • Friend, without posting the code it is difficult to help. Post the code of your controller, base.blade.php and the view that extends the base.

1 answer

0

Solved!

I didn’t have to touch my controller or model or view. Had not carried out the use of provideres and it was with it that I solved.

In my predecessor app\Providers\AppServiceProvider

I edited and left 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 model Alert, and with View::share('alerts', $alerts); I globally exported the alert array.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.