N
N
naneri2014-09-12 07:18:21
Laravel
naneri, 2014-09-12 07:18:21

How to make a query to the database for a single widget?

I am currently building a website in Laravel. The peculiarity is that it is necessary to make a sidebar in which the categories whose names are taken from the database will be displayed.
Where exactly to make a query to the database to pull out the names of these categories?
The peculiarity is that I don’t know in advance on which pages I will need a sidebar with categories, and it would be wrong to write such a request in the controller’s parent class right away ... Or is it right?
It's still not clear how to include it in all pages with templates...
Before that, the same problem arose when I worked with Codeigniter.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
K
kaluzhanin, 2014-10-19
@kaluzhanin

If still relevant, the most DRY approach is to use view composers . Every time you connect a sidebar via {{ View::make('sidebar') }}, variables in it can be filled in bypassing controllers in a class specially dedicated for this. Everything is detailed here .

K
Kir, 2014-09-12
@angry_bender

In a separate controller of course. And load with ajax.

P
Pavel, 2014-09-18
@PaulTMatik

All requests that will be used on most pages are best done in the base controller.
For the menu, in your case it is better to use a template engine. For example
file: main.blade.php

<html>
<body>
@yield('menu')
@yield('content')
</body>
</html>

page.blade.php file
@extends('main')

@section('menu')
@include('categories', ['categories'=>$categories])
@stop

@section('content')
<div>Content</div>
@stop

categories.blade.php file
<ul>
@foreach($categories as $category)
<li>{{$category->name}}</li>
@endforeach
</ul>

And in the controller ask
This way you can include menus whenever you want by simply creating a 'menu' section in the template.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question