2016-04-30 10 views
0

私は、laravelフレームワークの非常に新しくなっています。 私は送信ボタンをクリックして更新する必要がある1つのフォームを持っています。 送信ボタンをクリックすると、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

。あなたは特定のテーブルを更新する必要があると言いましたが、意図的に手動でデータベースエントリを更新する2つの行があります。

これはそのうちの一つである:

あなたがこれを呼び出す
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を介して間違って直接変更を適用している場合、モデルのsaveメソッドが上書きしています。

あなたのコードが実際に何をしているのかははっきりしていないようですが、各行が何をするのか理解し始めるとコードを単純化して追加することを強くお勧めします。あなたの質問は、Laravelが関係やモデルをどのように処理するかを理解せずに、サンプルをコピーして自分の仕事を追加することによる副産物だと思います。生のSQL文またはDB文を使用する理由はほとんどありません。

+0

ありがとうございますが、私はEODでいくつかの問題を修正しなければなりません。私はあなたの提案に従って挑戦的に始めます。 –

+0

私はもう一度援助が必要です、どうすればあなたとチャットを始められますか –

関連する問題