2016-04-30 27 views
0

我對laravel框架非常陌生。 我有一種形式,我需要更新提交按鈕單擊。 當提交按鈕點擊控制轉到controller.php的update()函數。 但我無法編輯任何字段的值。在提交按鈕上更新表格字段

這是我的代碼。

public function update($id) 
    { 
     //echo "<pre>";print_r(Input::all());exit; 

     $product = $this->product->find($id); 

     $input  = Input::only('designer', 'sku', 'name', 'display_name', 'description', 'price', 'main_category', 'sub_category', 'lead_time', 'sizing', 'woven', 'body_fabric', 'lining_fabric', 'fit', 'primary_color', 'secondary_color', 'care_label', 'neck_type', 'closure', 'trims', 'special_finishings', 'image1', 'image2', 'image3', 'image4', 'image5','top', 'combo_products', 'keywords', 'visibility', 'featured'); 
     //echo "<pre>";print_r($input);exit; 

     try 
     { 
      $this->adminNewProductForm->validate($input); 


     } catch(\Laracasts\Validation\FormValidationException $e) 
     { 
      return Redirect::back()->withInput()->withErrors($e->getErrors()); 


     } 

     $slug  = Str::slug(Input::get('name')); 

     $slug  = $this->product->getSlug($slug); 

     $input  = array_add($input, 'slug', $slug); 


     DB::transaction(function() use($product, $input) 
     { 

      $product->fill($input)->save(); 

      $stock_count = 0; 

      if(!empty(Input::get('xsmall_size'))) 
      { 
       $rows = DB::table('products_variants')->where('product_id', $product->id)->where('variant_name', 'XS')->get(); 

       $stock_count += Input::get('xsmall_stock'); 

       if(!empty($rows)) 
       { 
        DB::table('products_variants')->where('product_id', $product->id)->where('variant_name', 'XS')->update(array('variant_specs' => Input::get('xsmall_size'), 'price_change' => Input::get('xsmall_price'), 'total_stock' => Input::get('xsmall_stock'), 'stock_used' => 0)); 


       } else { 

        DB::table('products_variants')->insert(array('product_id' => $product->id, 'variant_name' => 'XS', 'variant_specs' => Input::get('xsmall_size'), 'price_change' => Input::get('xsmall_price'), 'total_stock' => Input::get('xsmall_stock'), 'stock_used' => 0)); 

       } 

      } 

$input = array(); 

      $input['flagship_status'] = Input::get('flagship_status'); 

      if(Input::get('flagship_status')) 
      { 

       $input['stock_count'] = Input::get('small_stock'); 

      }else { 

       $input['stock_count'] = $stock_count; 
      } 

      $product->fill($input)->save(); 

     }); 

     //echo "<pre>";print_r(Input::all());exit; 

     return Redirect::back()->withFlashMessage('Product Updated Successfully!'); 

    } 

此外,我不能理解,這一行是怎麼回事?因爲我在我的代碼中找不到驗證函數。

$this->adminNewProductForm->validate($input); 

我需要更新表產品沒有products_variants

回答

0

validate繼承自FormRequst類。

https://laravel.com/api/5.0/Illuminate/Foundation/Http/FormRequest.html#method_validate

你提供太多的代碼和信息太少。你說你需要更新一個特定的表,但是有兩行你非常有意地手動更新數據庫條目。

這就是其中之一:

DB::table('products_variants')->where('product_id', $product->id)->where('variant_name', 'XS')->update(array('variant_specs' => Input::get('xsmall_size'), 'price_change' => Input::get('xsmall_price'), 'total_stock' => Input::get('xsmall_stock'), 'stock_used' => 0)); 

當調用此:

$product->fill($input)->save(); 

它還保存「髒」(修改)模式,也屬於它,它可以包括products_variants關係。從它的聲音,你不正確地通過SQL直接應用更改,然後模型的保存方法覆蓋它。

你似乎不清楚你的代碼實際上在做什麼,我強烈建議你簡化它並在代碼中添加,因爲你開始明白每行代碼的作用。我認爲你的問題是複製一個例子並添加你自己的工作而不理解Laravel如何處理關係和模型的副產品。幾乎沒有理由使用原始SQL或DB語句。

+0

謝謝,但我必須通過EOD修復一些問題。我會根據你的建議勇敢地開始。 –

+0

我想要一個更多的幫助,我該如何與您開始聊天 –