Global queries in laravel 5


$categories = Categories::get(); return view('coolpage', ['categories' => $categories]);

Blade View:

<ul> @foreach ($categories as $category) <li><a href="{{ secure_url($category->alias) }}">{{ $category->title }}</a></li> @endforeach </ul>

This works perfect on individual views.

I want to be able to have these categories show up on every page (this will be used as a dropdown menu in the header navbar). I have app.blade.php that contains the header html and @yield('content'). The only way I can think of querying out the categories is somehow putting it in my routes files... which I'm pretty sure is not the way to go.

I'm hoping to get suggestions / examples.

-------------Problems Reply------------

One solution is to use Laravel View Composers to bind data to a view.

As described in the Laravel documentation, you can use a wildcard view composer to accomplish this.

First you would set up a service provider for view composers, with a closure-based composer that will apply to every page (the * wildcard symbol):

<?php namespace App\Providers;

use View;
use Illuminate\Support\ServiceProvider;

class ComposerServiceProvider extends ServiceProvider {

* Register bindings in the container.
* @return void
public function boot()
View::composer('*', function($view)

* Register
* @return void
public function register()


Then you'll need to add the service provider to the providers array in config/app.php. This will register this service provider so that the view composer is called on every page load.

Then you can bind a collection of all Categories to all views by adding it to the wildcard view composer like so:

public function boot()
View::composer('*', function($view)
$view->with('categories', \App\Categories::all());

all, what @jakeOpena says is true, View Composers is the best solution.

here is my example from route.php :

View::composer('layouts.left_navbar_menu', function($view) {
$menu = App::make('LeftNavMenu')->menuBilder();
$view->with('name', $menu);

my view "layouts/left_navbar_menu" is included in master template, like this:


Category:php Views:4 Time:2018-07-25
Tags: php laravel

Related post

  • Appending Two arrays in Laravel get() Query Builders 2015-01-11

    I have to queries in Laravel. First, to get a single post. Second, to get all comments. My problem is, I cannot append correctly the two arrays output of the get() in Laravel Query Builders. When I tried to join them, the post appeared 3 times since

  • How do you deal with polymorphism in a database? 2008-09-05

    Example I have Person, SpecialPerson, and User. Person and SpecialPerson are just people - they don't have a user name or password on a site, but they are stored in a database for record keeping. User has all of the same data as Person and potentiall

  • Mongo DB Design, embedding vs relationships 2010-09-28

    I'm building a simple accounting system where a user has many bills. Now I'm trying to decide if bills should be its own collection, or nested within the user. I'm leaning towards the former but I've NEVER done any noSQL stuff so I'm just going by tr

  • Trouble with SpringPad API 2011-05-25

    I'm trying to access date using the Springpad API. If I use the following link: I have no problem getting 2 recent results with the search term "J K Rowlings'. I wrote the following

  • how to architect achievements and badging with nosql 2011-11-04

    I currently have an social game app using mongodb for it's database. My question is what are some suggestions if I want to create a points and badging system. The business logic for achievements/badges could become quite complicated and are very ad-h

  • Cypher query to get nodes with given property values 2011-11-07

    Being new to Cypher Queries on Spring Data Graph, this may be quite trivial... I am looking for what would be the Cypher query to fetch all nodes that have a given value for a couple of properties. So, what would be ??? in the @Query annotation for t

  • Why do Column oriented databases such as Vertica/InfoBright/GreenPlum make a fuss of Hadoop? 2011-11-25

    What is the point in feeding an Hadoop cluster and using that cluster to feed data into a Vertica/InfoBright datawarehouse ? All thse vendor keep saying "we can connect with Hadoop", but I don't understand what's the point. What is the interest of st

  • How do I find disconnected nodes on neo4j with Cypher? 2011-12-29

    I am toying with neo4j and noticed that all Cypher queries need a starting point in the START clause. I was wondering how can I find all disconnected nodes using Cypher ? thanks --------------Solutions------------- If all your nodes are indexed (e.g.

  • Query logging for mySQL on shared server 2012-03-19

    I have a VPS with Dreamhost but the mySQL server is shared. I really want to start producing accessible logs of every mySQL query a particular site issues. I can hand roll this into my abstraction layer but I was curious is there was something like s

  • MS Access - GLOBAL search for keyword (in queries, forms, reports) 2009-10-22

    [This questions pertains to Access 2003] I need to add an additional criteria (b) to a query wherever another specific criteria (a) is being used, so I need to do a global search for incidences of (a) So, I can do a global search in VBA code, no prob

  • Make Wordpress Ajax calls work with global variables to reduce database queries 2012-03-29

    I posted this earlier on However, never got a reply. Hence, trying my luck here. I am hereby providing a detailed description of what I need and what I have done for this issue of mine. I am open to any workable solution

  • Laravel: adding a class to global.php 2014-04-23

    I would like to register some event listeners in my laravel project. To do so I added the following to the bottom of my start\global.php file: use UserLog; $userLog = new UserLog; Event::listen('sentinel.user.login', function (){ $logData = array('ev

  • Using Eloquent in Laravel for more advanced DB queries 2014-09-11

    I've been developing a laravel application the last couple months and it's my first ride with the framework. It's also my first time embracing and using an ORM (Eloquent). For the most part, Eloquent seems pretty straight forward when doing basic DB

  • Making a model available to the layout globally in Laravel 2015-02-04

    I am just starting out with Laravel and I'm putting together a quick blog site as an exercise. I have a posts model that has posts, with content. I can list the posts, display a single post, and create a post. I have created a categories table and mo

  • Global Query Scope and Relationship with Laravel 5 2016-06-19

    I'm trying to get familiar with eloquent and I've been playing around with some global query scopes, but i'm not having much success when it comes to the effect that it has on relationships. I have two models; product and category, each with some glo

  • Log all queries in mysql 2008-11-20

    Is it possible for me to turn on audit logging on my mysql database? I basically want to monitor all queries for an hour, and dump the log to a file. --------------Solutions------------- Start mysql with the --log option: mysqld --log=log_file_name o

  • Performing Interactive queries in MySql (mainly from the GUI) 2008-11-25

    I mainly use the MySQL GUI tools. This allows me to easily see the results in a table as well as to quick edits and bookmark frequently run queries. This suits my needs far better than the command line. I remember when I used to do this on Oracle DBs

  • Managing and debugging SQL queries in MS Access 2009-01-07

    MS Access has limited capabilities to manage raw SQL queries: the editor is quite bad, no syntax highlighting, it reformats your raw SQL into a long string and you can't insert comments. Debugging complex SQL queries is a pain as well: either you hav

  • Best way to store global variables 2009-01-13

    I'm writing an application in PHP that uses a LOT of global variables that are used throughout the script. Right now, I have a config file that stores a bunch of global variables created by using the define() function, but since I'm going to have so

  • UrlRewriting on Global.asax and SQL Output Caching 2009-02-01

    I'm performing a UrlRewrite for my main category pages. Converting: to In order to do so I'm using Global.asax's Application_BeginRequest where I perform the following code(pseudocode): protected

  • How can I view live MySQL queries? 2009-02-20

    I am just wondering if its possible to trace MySQL queries on my linux server as they happen? For example I'd love to set up some sort of listener, then request a web page and view all of the queries the engine executed, or just view all of the queri

  • T-SQL Global / Shared Data 2009-03-04

    When writing TSQL stored procedures I find myself wanting to centralize / normalize some parts of the code. For example, if I have a query like this: SELECT * FROM SomeTable WHERE SomeColumn IN (2, 4, 8) For cases like this I would like to put (2,4,8

  • Does more data mean slower queries? 2009-03-10

    Let's say I have one table that has 1000 rows and the other table with the same structure/index but 10 million records. Will the performance of CRUD operations on the bigger table be slower than the smaller one? Thanks. --------------Solutions-------

  • How to show the last queries executed on MySQL? 2009-03-16

    Is there any query/way to show the last queries executed on ALL servers? --------------Solutions------------- Additionally, for those blessed with MySQL >= 5.1.12: Execute SET GLOBAL log_output = 'TABLE'; Execute SET GLOBAL general_log = 'ON'; Tak

Copyright (C), All Rights Reserved.

processed in 0.113 (s). 11 q(s)