正在更新“提交”按钮上的表字段


Updating table fields on submit button

我对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!');
    }

我也不明白,这条线路是怎么回事?因为我在代码中找不到validate函数。

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

我需要更新表产品而不是products_variations

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语句几乎从来没有什么好的理由。