Send data only to sidebar.blade.php

1

I need to set up a menu using the registered categories. How can I do this without using a view ?

I have my layout.blade.php with a @include('sidebar')
My query works on view categorias.blade.php , I just need to receive query data on my sidebar.blade.php

CategoryController:

public function listar() {
    $categorias = Categoria::where('categoria_id', '0')->with('todasCategorias')->get();
    return view('categorias', compact('categorias'));
}

I made the direct query in sidebar.blade.php . It worked, but I think this is not the right way, does anyone know otherwise?

$categorias = App\Categoria::where('categoria_id', '0')->with('todasCategorias')->get()
    
asked by anonymous 16.09.2015 / 18:56

2 answers

0

I solved using the Service Injection of Laravel 5.1.

In my sidebar.blade.php I added:

@inject('categorias', 'App\Categoria')

@foreach($categorias->listar() as $categoria)
    <li>{{ $categoria->nome }}</li>
@endforeach

Documentation: link

    
04.10.2015 / 16:41
1

See the code below.

In the line of return use with , to say that you will load the page with the list of categories. You withCategorias . Categories is the name you will use in foreach in blade . And the variable inside is the variable that makes the query, $categorias .

public function listar() {
    $categorias = Categoria::where('categoria_id', '0')->get();
    return view('categorias')->withCategorias($categorias);
}

Then in blade you do:

@foreach($categorias as $item)
   $item->nome_do_campo
@endforeach

You can use the __construct()

public function __construct(){
   $categorias = Categoria::where('categoria_id', '0')->get();
   view()->share('categorias', $categorias);
}

Then apply the same concept above foreach in sidebar.blade.php .

    
16.09.2015 / 19:31