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
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.You can use it like this