Have you looked at the query builder documentation?
http://laravel.com/docs/5.0/queries
Are you just looking for something like this?
$settings = DB::table('settings')->firstOrFail();
Then pass $settings to your view where you can then access the columns in that table.
{{ $settings->name }}
I ended up doing something similar, I used:
$settings = DB::table('settings')->pluck('name');
But, your way may be better. So my next question is this, how can this be used in all views? For testing purposes I just did this:
return view('master')->with('settings', $settings);
I added a /test route, and that worked great when I go to /test, but how do you get it to display on all views? I assume you don't return a single view directly? But I'm not sure on what to return to display in all views. Is something like View::share considered good practice for this, or am I going down the incorrect path? Keeping in mind that I'm using a controller to run this code in, unless that is the incorrect approach in this case as well?
Thanks.
Edit: I guess maybe the more appropriate question would have been, how do you then pass $settings to the view? When I load the view I always get "Undefined Variable".
you can use View::share or view()->share but you should use that in boot() function at AppServiceProvider. Also you can use composer function.
Thank you ilkeruyank. That was actually the direction I was started to head. I've tried adding it, and as of yet, I've been unable to make it work. I blame only having been learning Laravel for a month. I'm sure I'll get it figured out, but if anyone has an example in mind, that would be fantastic. Thank you to all who have helped with this so far.
What I have so far in the controller:
public function __construct()
{
$settings = DB::table('settings')->pluck('name');
return $settings;
}
First I don't even know if I should be using a construct here, but the bigger question for me is the return value.
Then, in the Service Provider in the boot method I have:
view()->share('settings');
I'm sure I'm also incorrect on how to use view share properly. My assumption is that it it just grabs the variable name, but I'm betting that I'm incorrect.
Well after playing around, I do see that I was wrong on view share. When I add a second argument, it works.... except that it's not pulling data from the controller. So when I do this:
view()->share('settings', $settings);
I get an "undefined variable" error, but if I do this:
view()->share('settings', 'settings');
It works, except it obviously just adds the string value "settings". So how to pass the data from my controller so that the service provider receives it?
I ended up just adding the query into the app service provider. It works just fine, but is this best practice? Or should I still run the query from the controller? For the life of me, I couldn't figure out how to get the variable from the controller to the service provider. Even after using the namespace.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community