【发布时间】:2022-01-08 01:49:06
【问题描述】:
我正在尝试创建一个包含三个 slug 的路线,其中包括类别、品牌名称和产品名称。
web.php
Route::get('/shop/{category:slug}/{brand:slug}/{product:slug}', [ProductController::class, 'index']);
控制器
<?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'));
}
}
由于某种原因,我收到此错误,我不明白为什么。
BadMethodCallException 调用未定义的方法 App\Category::brands()
【问题讨论】:
-
奇怪,我在这里的代码中没有看到
brands()。请发布堆栈跟踪,以便我们查看错误所在。它可能在您的一个模型中。 -
您是否偶然在刀片文件中调用了
$category->brands? -
不,我不是,如果我从函数中删除
Brand $brand,它就可以正常工作。
标签: php laravel laravel-routing