A
A
Andriy Sub2019-10-08 18:53:35
Laravel
Andriy Sub, 2019-10-08 18:53:35

How to pass model to all views with __construct?

Controller from which everything is inherited

public $new_comments_moderate;

public function __construct()
{
    $this->categories = $categories;
    $this->companies_modal = $companies_modal;

    $comments_count = CompaniesComment::query()
        ->where('status', 0)
        ->count();

    $this->new_comments_moderate = $comments_count;
}

Layouts
{{$new_comments_moderate}}
how can I make the variable ($new_comments_moderate) available in layouts so as not to be passed to each view like this
return view('admin.dashboard')->with([
    'new_comments_moderate' => $this->new_comments_moderate
]);

Answer the question

In order to leave comments, you need to log in

2 answer(s)
J
JhaoDa, 2019-10-08
@JhaoDa

Just like it says in the documentation .

V
Vyacheslav Plisko, 2019-10-09
@AmdY

The correct option was suggested to you above by JhaoDa, to start a provider, extract and forward data to the desired template via View:: compose. The code that you stuck in the controller constructor smells, it doesn't belong there.
But if you need a quick and crooked crutch, you can View::share('new_comments_moderate', $this->new_comments_moderate);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question