I have defined slug to view a product. The slug destination works fine but all slugs return the same product (the first one) not the one should be.
It does not show required product when I browse. It shows the first record from database. What am I missing?
My Route
Route::get('/{slug}/order', 'Controller@order')->name('order')->where('slug', '[\w\d\-]+(.*)');
Route::post('/{slug}/order', 'Controller@order_store')->name('order_store')->where('slug', '[\w\d\-]+(.*)');
Route::get('/{slug}', 'Controller@view')->name('view')->where('slug', '[\w\d\-]+(.*)');
Controller
public function show($slug, request $id)
{
$item = Item::where('slug', $slug)->firstorfail()->where('status', true)->firstorfail();
return view ('products.show', ['item'=>$item]);
}
View Blade
<a target="_blank" href="{{ route('view', $item['slug']) }}">View</a>
For future reference:
You should not call firstOrFail() twice on a query builder.
Change
$item = Item::where('slug', $slug)->firstorfail()->where('status', true)->firstorfail();
To
$item = Item::where('slug', $slug)->where('status', true)->firstorfail();