A
A
Alfieros2021-11-25 12:59:50
Laravel
Alfieros, 2021-11-25 12:59:50

How to pass the brand id inside the ViewComposerService provider in order to get a list of child categories based on it?

I am making a global menu and trying to display categories that belong to the brand in the menu. In my case, the brand_id is stored in the categories table. The menu is made in a separate template and is connected on the brand page and on the category page. It is not possible to pass the value of the category parent (brand_id) in the request, it works as I want if I specify the id manually. Tell me how to do it right? Through the controllers for each template, I do not want to receive a list of categories by parent.

Category model:

public function brand()
    {
         return $this->hasMany(Brand::class, 'brand_id');
    }

Brand Model:
public function category() {
        return $this->hasMany(Category::class, 'brand_id');
    }


Composer query:
View::composer('site.partials.sidebar', function ($view) {
            $view->with('categories', Category::with('brand')->where('brand_id', 4)->get());  
           //выводит все категории бренда с id 4.
        });

Answer the question

In order to leave comments, you need to log in

2 answer(s)
J
jazzus, 2021-11-25
@mrsexy

https://laravel.com/docs/8.x/blade#components

P
pLavrenov, 2021-11-25
@pLavrenov

You are welcome.

View::composer('test.test', function ($view) {
    dd($view->getData());
});

So we return from the controller:
return view('test.test')->with([
   'test_variable' => 'ололо',
]);

Or so we import directly in the template.
@include('test.test', [
   'test_variable' => 'ололо',
])

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question