一对多关系表更新laravel

时间:2019-04-24 11:46:29

标签: php laravel

我有以下型号:

Page.php Content.php 一对多关系。

它们的表结构如下:

页面

id |标题| sl

内容

id | page_id | section_title | section_content

在我的编辑视图中,section_titlesection_content字段可以动态生成。

即:用户可以根据需要添加更多内容部分。

下面是编辑页面中的摘录:

@foreach ($page->content as $content)

<div class="row fieldGroup" id="dataRow{{$content->id}}">
  <input type="hidden" name="contentID[]" value="{{$content->id}}">
  <div class="col-sm-10  ">
    <div class="form-group floating-label {{$errors->has('sectionTitle') ? 'has-error' : ''}}">
      <input type="text" name="sectionTitle[]" id="sectionTitle" class="form-control" value="{{$content->sectionTitle}}"> @if($errors->has('sectionTitle'))
      <span class="help-block">{{ $errors->first('sectionTitle') }}</span> @endif
      <label for="sectionTitle">Section Title</label>
    </div>
  </div>
  <div class="col-sm-2  ">
    @if($loop->first)
    <a href="javascript:void(0)" class="btn btn-success addMore">
      <span class="glyphicon glyphicon glyphicon-plus" aria-hidden="true"></span> Add Section
    </a>
    @else
    <a href="javascript:void(0)" class="btn btn-danger removeData" id="id-delete" data-id="{{$content->id}}"><span class="glyphicon glyphicon glyphicon-remove" aria-hidden="true"></span> Remove</a> @endif
  </div>
  <div class="col-sm-12  ">
    <div class="form-group">
      <h4>Section Content</h4>
      <textarea name="sectionContent[]" class="editor">{{$content->sectionContent}}</textarea>
    </div>
  </div>
</div>
@endforeach

下面是来自控制器的update方法的代码段:

$page - > title = $request - > title;
$page - > slug = Str::slug($request - > title, '-');
$page - > save();

$contents = Content::where('page_id', $page - > id) - > get();
for ($i = 0; $i < count($request - > sectionTitle); $i++) {
  foreach($contents as $db) {
    if (isset($request - > contentID[$i]) && $db - > id == $request - > contentID[$i]) {
      $content = Content::find($request - > contentID[$i]);
      $content - > sectionTitle = $request - > sectionTitle[$i];
      $content - > sectionContent = $request - > sectionContent[$i];
      $content - > save();
    } else {
      $new = new Content;
      $new - > page_id = $page - > id;
      $new - > sectionTitle = $request - > sectionTitle[$i];
      $new - > sectionContent = $request - > sectionContent[$i];
      $new - > save();
    }
  }
}

我遇到的麻烦是,如果用户在编辑内容后单击更新按钮。每个内容被保存一次以上。

即:如果有3个内容,则每个内容将保存3次

1 个答案:

答案 0 :(得分:0)

之所以会这样,是因为您有两次循环,请用下面的代码替换

$page-> title = $request->title;
$page-> slug = Str::slug($request->title, '-');
$page-> save();

$contents = Content::where('page_id', $page->id)->get();

for ($i = 0; $i < count($request-> sectionTitle); $i++) {

    if (isset($request->contentID[$i])) {
        Content::updateOrCreate([
            'id' => $request->contentID[$i];
            'page_id' => $page->id;
            'sectionTitle' => $request->sectionTitle[$i],
            'sectionTitle' => $request->sectionTitle[$i],
            'destination' => $request->sectionContent[$i]],
        );
    } else {
        $new = new Content;
        $new-> page_id = $page->id;
        $new-> sectionTitle = $request->sectionTitle[$i];
        $new-> sectionContent = $request->sectionContent[$i];
        $new-> save();
    }
}

希望这对您有帮助

相关问题