2016-01-21 40 views
1

如何使Form :: model具有多個參數? 我有這樣的例子路線。 www.domain.com/product/2/product-attributes/3/edit 在正常的形式,我可以做這樣的:L5表單模型綁定多個參數

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

,但如果我嘗試使用laravelcolective/HTML表單::模型( )是這樣的:

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

我得到一個錯誤數組字符串轉換...

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

條我的路線:

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

=====更新2 =========================

已經解決,但我會證明我的路線並沒有錯 PHP工匠路線:列表 enter image description here

回答

2

IM只是想..如果正常的路由使用數組來傳遞多個參數。我想表::模型不需要使用數組只是分開,

正常路線::

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

表型號::

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

首先,我從來沒有見過一個路徑名以花括號中它喜歡你:

product.{product}.product-attributes.update

因此不確定服務的目的是什麼?也許你可以與我們分享你的路線?

對我來說,我不喜歡這樣......

假設你有一個路線:

Route::get('product/{product_id}/product-attributes/{product_atrribute_id}/edit',[ 
    'as' => 'product.product-attributes.update', 
    'uses' => '[email protected]', 

]); 

然後,你可以打電話給你的路線的方法是這樣的:

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

這裏的區別(在你調用方法和我的方式之間)是我已經指定了匹配url中的鍵的數組鍵。

至於打開表單我想你會做此鏈接:

(請注意,我做了一些假設,你有沒有共享很多代碼的)

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 
       ] 
      ] 
     ] 
    ] 
) 

請注意,在代碼中的錯誤:product->id應該是:$product->id

+0

它只是錯字:X,我已經更新我的路線..我使用路線::資源。除了當我使用Laravelcollective HTML/Form時,我沒有任何問題:Form :: model – GandhyOnly

+0

如果您使用的是路由資源,請運行'artisan route:list'來獲取路由的正確名稱,我很漂亮肯定它不是'產品。{product} .product-attributes.update' – haakym

+1

解決了,但如果你問我的路線:列表來證明我的路線,你可以看到我的更新@haakym – GandhyOnly

0

它看起來像你應該使用「嵌套資源」(調整命名爲需要)

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

路線(顯示):

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

Laravel Docs - Nested Resource Controllers

+0

我對路由沒有任何問題...我只是遇到Form :: model for form模型綁定問題 – GandhyOnly