Laravel 5 how to use parameter variable correctly in my view
I have been trying to learn laravel since 3 days and I have a little problem.
In my database, I have a 'settings' table that looks like this:
I need to use this data on every page I load. I am doing something like this to load data in my opinion:
public function indexFront()
{
$posts = $this->blog_gestion->indexFront($this->nbrPages);
$links = str_replace('/?', '?', $posts->render());
//Here i load my setting
$setting_gestion = new SettingRepository(new setting());
$config = $setting_gestion->getSettings('ferulim');
//Next i pass my setting to my view
return view('front.blog.index', compact('posts', 'links', 'config'));
}
It works, but I need to do it in every controller and every function ... I think there is another way to do the same thing: p
Help me!
source to share
You can easily achieve this for selected views or all views using View Composers ( http://laravel.com/docs/5.0/views#view-composers ).
First, you need to implement a view composer that does the logic you want to do for each view:
<?php namespace App\Providers;
use View;
use Illuminate\Support\ServiceProvider;
class ComposerServiceProvider extends ServiceProvider {
public function boot()
{
View::composer('*', function($view) {
$blog_gestion = ...; // fetch or instantiate your $blog_gestion object
$posts = $blog_gestion->indexFront($this->nbrPages);
$links = str_replace('/?', '?', $posts->render());
$setting_gestion = new SettingRepository(new setting());
$config = $setting_gestion->getSettings('ferulim');
$view->with(compact('posts', 'links', 'config'));
});
}
public function register()
{
//
}
}
Then register a new service provider in the providers array in config / app.php .
What is it :) Let me know if there are any typos / errors, I haven't had a chance to run the code.
source to share