我的控制器认为我的商店 ID 是产品 ID

My controller considers that my store id is the products id

我的控制器无法读取 'sample' 是商店而不是产品。 我的 web.php

上有这条路线
    Route::get('{store}/products/{products}/edit', [
    'as'    => 'store.products.edit',
    'uses'  => 'ProductsController@edit',
    function($store) {
        $store = App\Models\Store::where('slug', $store)->firstOrFail();
    }
]);

这是我的 ProductsController@edit

    public function edit($id)
{
    $product = Product::findOrFail($id);

    return view('view here', compact('product'));
}

当我 运行 url: http://127.0.0.1:8000/sample/products/022fe902-7f4d-4db1-b562-04a7eb9f5a68/edit

其中 sample 是 {store},022fe902-7f4d-4db1-b562-04a7eb9f5a68 是 {product}

我收到这个错误:

No query results for model [App\Models\Product] sample

在我的查询中:

select * from products where products.uuid = 'sample' limit 1

如果你有 2 个参数,你应该在你的控制器中以有效的顺序将它们都放入,所以你应该:

public function edit($store, $id)
{
    $product = Product::findOrFail($id);

    return view('view here', compact('product'));
}

也可能你不需要这里:

function($store) {
   $store = App\Models\Store::where('slug', $store)->firstOrFail();
}

对于任何事情,但也许在你的控制器中你应该做这样的事情:

$store = App\Models\Store::where('slug', $store)->firstOrFail();
$product = $store->products()->findOrFail($id);

假设您在这家商店中有产品,并且您希望确保有人不会编辑分配给其他商店的产品。