1

I'm confused or been to much in the sun, I have the following situation when editing a product

http://app.dev/shops/1/products/2/edit

I added the /shops/1 in the URL because I need to know for what Shop I'm editing the Product.

Now in the Controller I need to know what Shop and Product we're talking about here. I'm using the following hidden input field to send the product_id to the update Controller

<input type="hidden" name="product" value="{{ $product->id }}">

But how do I get the shop_id to the Update Controller. What's the best way to go about this?

Thank you!

2
  • <input type="hidden" name="shop" value="{{ $shop->id }}"> and get product and shop values in controller Commented May 17, 2017 at 12:15
  • @BilalAhmed the $shop variable isn't available in this view. It's an edit product view. Commented May 17, 2017 at 12:17

3 Answers 3

2

route file

Route::get('/shops/{shopid}/products/{productid}' , 'testController@gettest');

In controller

Input::get('shops');
Input::get('products');

and also check this

Route::get('/', function(){

    echo Input::get('shops');
    echo Input::get('products');
});
Sign up to request clarification or add additional context in comments.

Comments

1

laravel way

 {{ Request::segment(3) }} 

will give you the id and you can pass it either in hidden input or as you want

PHP way

$actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

$shop = explode('/', $actual_link);
$shop_id = $shop[1/2/3 or 4]; 

//depending in which position the shop id is coming, you can check it by printing $shop

2 Comments

I have a feeling that when using the Request::segment'ish things that my data is wrong or my url isn't being built correctly. doesn't seem best practice, don't know why
ok so you need anything else because you dont like this? i have other options too.
0

So you have a route similar to this

Route::post('/shops/{shop_id}/products/{product_id}/edit' , 'TestController@edit');

And in your controller you can simply use. Now you have both ids and can do what you want.

public function edit($shop_id, $product_id) {
    //do what you want
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.