3

How to make Form::model with multiple parameter? i have example route like this. www.domain.com/product/2/product-attributes/3/edit in normal form i can do it like this:

<form method="POST" action="{{ route('product.{product}.product-attributes.update', [$product->id, $product-attribute->id ]) }}"> </form>

but if im trying to use laravelcolective/html Form::model() like this:

Form::model($productattributes, array('method' => 'POST', array('route' => array('product.{product}.product-attributes.update', [$product->id, $product-attribute->id ])))

i get an error array to string conversion...

==== UPDATE ==============

My Routes:

Route::resource('backend/product', 'Backend\ProductController');
Route::resource('backend/product/{product}/product-attributes', 'Backend\ProductAttributesController',['except' => ['index']]);

===== UPDATE 2 =========================

already solved but i will prove my route not wrong php artisan route:list enter image description here

3 Answers 3

4

im just trying.. if normal route use array to pass multiple parameter. i guess Form::model dont need to use array just separate with ,

Normal Route ::

<form method="POST" action="{{ route('product.{product}.product-attributes.update', [$product->id, $product-attribute->id ]) }}"> </form>

Form Model ::

Form::model($productattributes, array('method' => 'POST', array('route' => array('product.{product}.product-attributes.update', $product->id, $product-attribute->id)))
Sign up to request clarification or add additional context in comments.

Comments

2

Firstly, I've never seen a route name with curly braces in it like yours:

product.{product}.product-attributes.update

So unsure what purpose that serves? Maybe you can share your route with us?

For me I'd do it like this...

Say you have a route:

Route::get('product/{product_id}/product-attributes/{product_atrribute_id}/edit',[
    'as'   => 'product.product-attributes.update',
    'uses' => 'ProductController@updateProduct',

]);

Then you can call your route method like this:

route('product.product-attributes.update', 
    ['product_id' => product->id, 'product_atrribute_id' => $product-attribute->id ]
)

The difference here (between the way you call the method and I have) is that I've specified the array keys that match the keys in the url.

As for opening the form I think you'd do it link this:

(Please note I've made some assumptions as you haven't shared much of your code)

Form::model( 
    // pass it the product so the product id can be accessed directly 
    // and the attributes id can be accessed through relationship method
    $product, 
    [
        'method' => 'POST', [
            'route' => [
                'product.product-attributes.update', [
                    'product_id' => $product->id, 
                    'product_attributes_id' => $product->attribute->id
                ]
            ]
        ]
    ]
)

Please note the mistake in your code: product->id should be: $product->id

4 Comments

it's just typo :x, I already update my routes.. i'm using Route::resource. I dont have problem on normal form except when I using Laravelcollective HTML/Form : Form::model
If you're using a route resource, run artisan route:list to get the correct name of your route, I'm pretty sure it's not product.{product}.product-attributes.update
solved but if you ask of my route:list to prove my route like that you can see my update @haakym
Thanks, didn't realise you could have a route name with {} in it, or at least that's how it would be generated
1

It looks like you should be using a 'nested resource' (adjust naming as needed)

Route::resource('backend/product.attributes', 'Backend\ProductAttributesController',['except' => ['index']]);

Route (Show):

backend/product/{product}/attributes/{attributes}

Laravel Docs - Nested Resource Controllers

1 Comment

i have no problem with the route... i just have problem with the Form::model for form model binding

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.