Tuesday, May 23, 2017

How to pass helper data to all views in Laravel 5 using helper in AppServiceProvider?

I would like to move a helper to be displayed in all views. The helper is ->

helperFunctions::getPageInfo($cart, $total);

At this moment I have to define in every controller that information, as an example:

public function show($id, Request $request)
{
    $category = Category::find($id);
    if (strtoupper($request->sort) == 'NEWEST') {
        $products = $category->products()->orderBy('created_at', 'desc')->paginate(40);
    } elseif (strtoupper($request->sort) == 'HIGHEST') {
        $products = $category->products()->orderBy('price', 'desc')->paginate(40);
    } elseif (strtoupper($request->sort) == 'LOWEST') {
        $products = $category->products()->orderBy('price', 'asc')->paginate(40);
    } else {
        $products = $category->products()->paginate(40);
    }
    helperFunctions::getPageInfo($sections, $cart, $total);
    return view('site.category', compact('cart', 'total', 'category', 'products'));
}

I read and tried to move that helper to AppServiceProvider.php, inside the Boot() function.

public function boot()
{

    helperFunctions::getPageInfo($cart,$total);
    View::share('cart','total');
} 

But im receiving error info:

Undefined variable: total



via s_h

Advertisement