Laravel更新方法不起作用

时间:2016-08-02 10:42:00

标签: php sql laravel foreign-keys where

我正在尝试更新我的数据库,但它无法正常工作。

我构建了一个更新方法,问题是它不断用product_id更新所有记录,而它应该更新每条记录 我在我的数据库中有10个不同值的记录。

如果我运行脚本,所有记录都会从输入字段中获取第一个值。

如何使其正常工作?

我的数据库看起来有点像这样:

id      |       product_id       |       short_name      |      input_video
___________________________________________________________________________
 1      |            49          |            de         |       c:\...
 2      |            49          |            en         |       c:\...
 3      |            49          |            tr         |       c:\...
 4      |            49          |            dr         |       c:\...

如果我运行我的代码(选择de),它看起来像这样:

id      |       product_id       |       short_name      |      input_video
___________________________________________________________________________
 1      |            49          |            de         |       c:\...
 2      |            49          |            de         |       c:\...
 3      |            49          |            de         |       c:\...
 4      |            49          |            de         |       c:\...

代码:

    # save language selection
    $lsCounter = 0;
    $langSelecName = $request->input('language_selection');
    $langSelecFile = $request->file('language_selection');
    if($langSelecName)
    {
        $projectLangPath = $Path . "language";

        foreach($langSelecName as $langSelecNameKey => $langSelecNameValue)
        {
            if($langSelecFile[$lsCounter]['input_vid_lang'] != null)
            {
                $langVidFileName = $langSelecFile[$lsCounter]['input_vid_lang']->getClientOriginalName();

                $languages = new Language();
                $languages['short_name']  = $langSelecNameValue;
                $languages['input_video'] = $projectLangPath . '\\' . $langVidFileName;

                $languages->product()->associate($product);

                $langSelecName = $request->input('language_selection');

                $langData = [
                    'short_name' => $languages['short_name'],
                    'input_video' => $languages['input_video']
                ];

                $intProductID = intval($productID);

                $findLang = $languages->where('product_id', $intProductID);

                $productID = $data['id'];

                if($findLang->update($langData))
                {
                    $langSelecFile[$lsCounter]['input_vid_lang']->move($projectLangPath, $langVidFileName);
                }
            }

            $lsCounter++;
        }
    }

我的where子句可能不对,但我不确定如何修复它。

修改 我的模特:

<?php

    class Language extends Model
    {

        protected $table = 'products_languages';

        protected $fillable = ['product_id', 'short_name', 'input_video'];

        public function product()
        {
            return $this->belongsTo('App\Product', 'product_id');
        }

    }

修改

查看:

            <fieldset class="form-group">
                <select class="form-control" id="language_selection" name="language_selection[]" multiple>
                    @foreach($languages as $languageKey => $languageValue)
                        <option value="<?php echo $languageValue->short_name; ?>"><?php echo $languageValue->name; ?></option>
                    @endforeach
                </select>
            </fieldset>
            @if($type == "edit")
                <input name="id" type="hidden" value="{{ $productId }}">
                @if($languagesCount > 0)
                    @foreach($languages as $languagesKey => $languagesValue)
                        <?php $i = 0 ?>
                            <span class="btn btn-primary btn-file lang-edit">{{ strtoupper($languagesValue->short_name) }}</span>
                        <?php $i++ ?>
                    @endforeach
                @endif
            @endif

2 个答案:

答案 0 :(得分:0)

看起来这里的订单有点混乱:

        $intProductID = intval($productID);

        $findLang = $languages->where('product_id', $intProductID);

        $productID = $data['id'];

你可能意思是这样做:

        $productID = $data['id'];

        $intProductID = intval($productID);

        $findLang = $languages->where('product_id', $intProductID);

与以下内容相同:

        $findLang = $languages->where('product_id', intval($data['id']));

因为我没有看到你在其他任何地方使用这些ID。

答案 1 :(得分:0)

我重新格式化并重命名并重做了很多工作。我的假设在最顶层的评论中定义

// $product_id = 1 
// $lang_select_name = ["TR"] => on create it was ["DE", "EN"] and is now updated with TR only
// $lang_select_file = [<fileTR>] => on create it was [<fileDE>,<fileEN>]
// $projectLangPath = '' // whatever your path is

// first of all get the product 
$product = Product::find($product_id);

// list all existing short_names for product id
$existing = $product->languages->lists('short_name');

// tbd: delete every entry that is existing but not in $lang_select_name

// loop through given input of names 
for($i = 0; $i < count($lang_select_name); $i++) 
{
    // check that file is not null => should be done in validation if required
    if($lang_select_file[$i]['input_vid_lang'] != null)
    {
        // get the filename
        $vid_name = $lang_select_file[$i]['input_vid_lang']->getClientOriginalName();

        // first check if the language entry exists already
        $lang = Language::where('product_id', '=', $product_id)->where('short_name', '=', $lang_select_name[$i])->first();

        if(!$lang) {
            // create it 
            $lang = Product->languages()->create([
                "product_id" => $product_id,
                "short_name" => $lang_select_name[$i],
                "input_video" => $projectLangPath . '/' . $vid_name
            ]);
        } else {
            // update
            $lang->input_video = $projectLangPath . '/' . $vid_name;
            $lang->save();
        }
        // move the file
        $lang_select_file[$i]['input_vid_lang']->move($projectLangPath, $vid_name);
    }
}

基本步骤是:

  • 获取具有给定产品ID的产品
  • 获取给定产品的所有已存在的语言
  • 比较语言(短名称)与支配语言的输入
  • tbd:删除输入中未提供的所有语言
  • 循环输入的短名称
    • foreach短名称获取给定product_id的匹配语言
    • 如果找不到创建一个,否则更新现有
  • 移动文件(也可以检查是否有任何更改 - 然后不要重新创建)