2016-11-03 48 views
0

form.blade.php就像Laravel形模型

<div class="form-group"> 
    {!! Form::label('title', 'title :', ['class' => 'awesome']) !!} 
    {!! Form::text('product[title]', null, ['class' => 'form-control']) !!} 
</div> 

<div class="form-group"> 
{!! Form::label('description', 'description : ', ['class' => 'awesome']) !!} 
{!! Form::text('product[description]', null, ['class' => 'form-control']) !!} 

<div id="phone" class="form-group"> 
    {!! Form::label('reference_id1', 'reference_id1 : ', ['class' => 'awesome']) !!} 
    {!! Form::text('product[reference_id1]', null, ['class' => 'form-  control']) !!} 
</div> 

    <div class="form-group"> 
     {!! Form::label('category_id', 'category_id : ', ['class' => 'awesome']) !!} 
     {!! Form::select('category[]', $categories,null, ['class' =>  'form-  control', 'multiple']) !!} 
    </div> 
<div class="form-group"> 
    {!! Form::label('color', 'color : ', ['class' => 'awesome']) !!} 
    {!! Form::text('feature[0][color]', null, ['class' => 'form-control']) !!} 
</div> 
<div class="form-group"> 
    {!! Form::label('height', 'height : ', ['class' => 'awesome']) !!} 
    {!! Form::text('feature[0][height]', null, ['class' => 'form-control']) !!} 
</div> ` 

和我Edit.blade.php某事象

{!! Form::model($product,['method' => 'PATCH', 'action' => ['[email protected]',$product->id]]) !!} 
    @include('products.form', ['submitBtn' => 'submit']) 
{!! Form::close() !!}  

這我[email protected]

public function edit($id) 
     { 
     $product = Product::with('feature')->findOrFail($id); 
     $categories = Category::pluck('title','id'); 
     return view('products.edit')->withProduct($product)->withCategories($categories); 
}  

這是當我想編輯產品時,輸入請求被設置爲空! 例如,當我去http://myLarave/Public/product/2/edittitle和其他投入是空的:( 什麼建議?

回答

0

在你route.php或web.php取決於你laravel的版本,可以使ARG {id?},例如:

Route::get('edit/{id?}', '[email protected]'); 

和編輯功能,您可以初始化變量的$ id = null或空:

public function edit($id = null) 
{ 
    if($id != null){ 
     $product = Product::with('feature')->findOrFail($id); 
     $categories = Category::pluck('title','id'); 
     return view('products.edit')->withProduct($product)->withCategories($categories); 
    } 
} 
+0

我用 路線:: resource('product','ProductController',[ 'names'=> ['index'=>'products_path', 'show'=>'product_path', 'create'=>'create_product_path', 'edit'=>'edit_path'], ]); 在我的web.php文件中(路徑文件) – Omid

+0

你可以嘗試只使用空值''''或'null'來初始化'$ id',並且如果'$ id'不爲'null'或爲空則進行所有操作。 –

+0

其實我的'$ id'不是空的,非常感謝您檢查id – Omid