skip to Main Content

I’m trying to pass the $product variable in this controller to multiple view files.

function show($lang, $slug)
{
    $product = Product::where('slug', $slug)->firstOrFail();

    $mightAlsoLike = Product::where('slug', '!=', $slug)->MightAlsoLike()->get();

    return view('shop_show')->with(['product' => $product, 'mightAlsoLike' => $mightAlsoLike]);
}

I’m using view composer in AppServiceProvider to pass the data to multiple view files but the problem is that the data in the variable needs the $slug param. how do I pass that $slug param to AppServiceProvider?

 public function boot()
{
    View::composer(['shop_show', 'partials/menus/right_bar'], function ($view) {
        $site_slug = Route::current()->parameter('slug');

        $product = Product::where('slug', $slug)->firstOrFail();

        $view->with(['product' => $product]);
    });
}

EDIT: the $site_slug variable returns null.

SOLVED: I defined my $slug param as {product} in my routes. changing the $site_slug variable to $site_slug = Route::current()->parameter('product'); fixed the issue.

2

Answers


  1. Chosen as BEST ANSWER

    I defined my $slug param as {product} in my routes. changing the $site_slug variable to $site_slug = Route::current()->parameter('product'); fixed the issue.


  2. You can use it like this

    public function boot()
    {
       
        view()->share('categoryList', $categoriesList);
        view()->share('packageList', $packageList);
        view()->share('testList', $testList);
        view()->share('testList1', $testList1);
    
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search