Ad

My Controller Considers That My Store Id Is The Products Id

- 1 answer

My controlller can't read that the 'sample' is a store and not a product. I have this Route on my web.php

    Route::get('{store}/products/{products}/edit', [
    'as'    => 'store.products.edit',
    'uses'  => '[email protected]',
    function($store) {
        $store = App\Models\Store::where('slug', $store)->firstOrFail();
    }
]);

and here is my [email protected]

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

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

when I run the url: http://127.0.0.1:8000/sample/products/022fe902-7f4d-4db1-b562-04a7eb9f5a68/edit

where sample is the {store} and 022fe902-7f4d-4db1-b562-04a7eb9f5a68 is the {product}

I get this error:

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

in my query:

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

Ad

Answer

If you have 2 parameters, you should have them both in your controller in valid order, so you should have:

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

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

Also probably you don't need here:

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

for anything, but maybe in your controller you should do something like this:

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

Assuming you have product in this store and you would like to make sure that someone won't edit product that is assigned to different store.

Ad
source: stackoverflow.com
Ad