我正在尝试更新我的数据库,但它无法正常工作。
我构建了一个更新方法,问题是它不断更新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:'...
如果我运行我的代码(pick-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
看起来这里的订单有点乱:
$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']));
因为我没看到你在其他地方使用这些身份证。
我重新格式化、重命名和返工了很多。我的假设在顶部的评论中进行了定义
// $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的匹配语言
- 如果找不到,请创建一个,否则更新现有
- 移动文件(也可以检查是否没有任何更改,然后不要重新创建)