Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extend the core and show the data in every page in Laravel

Following the guideline, I have

1) Create a service provider

class ViewComposerServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
        view()->composer('template', function ($view) {
            $view->with('series_list', Series::all());
        });
    }

    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}

2) Register provider

 App\Providers\ViewComposerServiceProvider::class,

3) And echo the variable at the template

{!! var_dump($series_list)  !!}

The problem is :

Route::get('/', 'HomeController@index');
Route::get('product/view/{id}', 'ProductController@view');
Route::get('product/detail/{id}', 'ProductController@detail');
Route::get('/page/contact', 'PageController@contact');

PageController and HomeController can show the $series_list, but ProductController will return error:

Undefined variable: series_list in the template

Here is the ProductController:

class ProductController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        //$this->middleware('auth');
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function view($id = 1)
    {
        return view('product/list');
    }

    public function detail($id = 1)
    {
        return view('product/detail');
    }
}

Thanks a lot for helping.

like image 930
user782104 Avatar asked May 23 '16 15:05

user782104


People also ask

How to pass data to a view in Laravel?

Laravel provides different ways to pass data to a view. We can pass data directly from routes or through the controller. Here are some of the ways we can pass data to the view: 1. Using view (): We can directly pass the data in the ‘ view () ’ helper function by using the second parameter in the function which takes an array as key and value pair.

How do I customize Laravel?

To customize laravel you will need to take the following steps: Show data with pagination Make model: To fetch data from the database table you want to create one model. Remember the model name is the same as the table name in the database that wants to display.

How do I get the current page in Laravel?

By default, the current page is detected by the value of the page query string argument on the HTTP request. This value is automatically detected by Laravel, and is also automatically inserted into links generated by the paginator.

How to add a page view counter in a Laravel application?

Implementing a page view counter into your Laravel application seems like the easiest job at first. Just add a column in your database with the current view count and increment that on every page load, right?


1 Answers

Another way, a bit shorter

class AppServiceProvider extends ServiceProvider
{
    // other methods

    public function boot()
    {
        $series = Series::all();
        view()->share('series_list', $series);
    }

    // other methods

}
like image 85
huuuk Avatar answered Sep 20 '22 22:09

huuuk