I'm trying to create a route with three slugs, which include category, brand name and product name.
web.php
Route::get('/shop/{category:slug}/{brand:slug}/{product:slug}', [ProductController::class, 'index']);
Controller
<?php
namespace App\Http\Controllers;
use App\Brand;
use App\Category;
use App\Product;
use Illuminate\Http\Request;
class ProductController extends Controller
{
public function index(Category $category, Brand $brand, Product $product)
{
$product = Product::where('id', $product->id)->with('related', function($q) {
$q->with('brand')->with('categories');
})->with('brand')->first();
return view('product', compact('product', 'category'));
}
}
For some reason i get this error, which i don't understand why.
BadMethodCallException Call to undefined method App\Category::brands()
brands()in your code here. Please post the stacktrace so we can see where the error lies. It might be in one of your models.$category->brandsin your blade file by chance?Brand $brandfrom the function it works fine.