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

时间:2018-08-25 16:25:31

标签: php laravel laravel-5 laravel-routing

我的控制者无法读取“样品”是商店而不是产品。 我的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'));
}

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

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

我收到此错误:

  

没有关于模型[App \ Models \ Product]示例的查询结果

在我的查询中:

  

products中选择*,其中productsuuid =“样本”限制1

1 个答案:

答案 0 :(得分: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);

假设您在此商店中有商品,并且要确保不会有人编辑分配给其他商店的商品。