2016-12-05 58 views
2

希望有人能幫助我。我習慣了程序代碼,並且我正在將一箇舊的PHP應用程序重構爲Laravel。舊應用中的頁面有一個表格佈局,每個td都有一個輸入。這些輸入都是一個數組,使用jQuery,你可以在表中添加一個新行來創建一條新記錄。這是一個jsfiddle:https://jsfiddle.net/5tjk7jza/2/。它看起來有點像這樣,給你一個想法,但小提琴將有很大的幫助:Laravel表/創建和編輯相同的形式?

<tr> 
    <td><input type="text" id="name[]" size="15" name="name[]"></td> 
    <td><input type="text" id="heartrate[]" size="1" name="heartrate[]"></td> 
    <td><input type="text" id="intensity[]" size="1" name="intensity[]"></td> 
    <td><textarea rows="4" cols="50" id="description[]" name="description[]"></textarea></td> 
</tr> 

當它保存,它貫穿於循環更新基於其在每個ID的舊記錄數組達到由底部隱藏輸入指定的最大值。然後它插入新的記錄。

我想這樣做使用Laravel和窗體模型綁定,並沒有單獨的意見。我真的很喜歡這個添加/編輯在同一個表/視圖。對於我的生活,儘管閱讀文檔和谷歌搜索,我找不到解決方案。我已經看過同步,但這隻適用於多對多關係/數據透視表,附加功能無效,保存功能無效。誰能幫我嗎?謝謝!!愛Laravel到目前爲止!

編輯:如果我只是放棄形式模型綁定,並做了像這樣的人:How to validate multiple records insertion in laravel?。這是'錯誤'嗎?我可以遍歷所有「舊」記錄,然後插入所有新記錄,但是這感覺很像不使用框架和過程代碼。

編輯2:在updateorcreate方法遇到一些麻煩...我得到一個錯誤「preg_replace():參數不匹配,模式是字符串,而替換是數組。任何人都可以看看這個並幫助我嗎?我一直在看文檔,現在幾個小時...

這裏是我的表單/視圖:

{!! Form::model($workoutcategories,['method' => 'PATCH', 'url' => 'workoutcategories/update']) !!} 

@foreach($workoutcategories as $workoutcategory) 
<tr> 
    <td>{!! Form::text('name[]', $workoutcategory->name, ['class' => 'form-control', 'size' => '15']) !!}</td> 
    <td>{!! Form::text('heartrate[]', $workoutcategory->heartrate, ['class' => 'form-control', 'size' => '1']) !!}</td> 
    <td>{!! Form::text('intensity[]', $workoutcategory->intensity, ['class' => 'form-control', 'size' => '1']) !!}</td> 
    <td>{!! Form::textarea('description[]', $workoutcategory->description,['class' => 'form-control', 'rows' => '4', 'cols'=>'100']) !!}</td> 
</tr> 
    @endforeach 

</tbody> 
</table> 

這裏是我的控制器:

public function CreateOrUpdateCategory() 
{ 
    $workoutcategories = WorkoutCategory::all(); 
    return view('workouts.categoryform', compact('workoutcategories')); 
} 

public function update(WorkoutCategory $workoutCategory, WorkoutCategoryRequest $request) { 
    $cats = WorkoutCategory::all(); 
    $workoutCategory->updateorcreate(['name' => $request->name],['name' => $request->name, 'intensity' => $request->intensity, 'heartrate' => $request->heartrate,'description' => $request->description]); 
    return redirect('workoutcategoies'); 
} 
+1

也許它可以幫助:https://laravel.com/docs/5.3/eloquent#other-creation-methods –

+0

assumissing每一行('tr')是DB一個單獨的記錄,那麼你可以更新/只能通過主鍵添加該字段。 –

+0

'updateOrCreate'會像你的添加/編輯? – Wistar

回答

0

很多後來自Stack的其他問題的幫助,主要來自@Wistar + @MahfuzulAlam的提示/指針,我找到了解決方案。我需要使用createOrUpdate,但在名稱字段中使用它就像上面的列一樣不起作用,因爲如果您在其中一個字段上更改名稱,它將創建一個新字段。在每個輸入數組中需要一個隱藏的ID字段來檢查是否需要更新或創建。

這是我的看法。當建立你的看法,請確保您的FORM密切和FORM開放任何其他元素(特別是表)

@section('content') 
    <script type='text/javascript'> 
     $(window).load(function(){ 
      var count = 2; 

      $("#Add").click(function() { 

       var $clone = $("#tbl tbody #clone").clone(); 
       $clone.attr({ 
        id: "cloned", 
        style: "" // remove "display:none", 
       }); 

       $("#tbl tbody").append($clone); 
       $("#count").val(count); 
       count++; 
      }); 
     }); 
    </script> 

    <h1>Workout Zones</h1> 
    @include('errors/list') 

    <hr> 
    {!! Form::model($workoutcategories,['method' => 'POST', 'url' => 'workoutcategories/update']) !!} 
    <table class='table table-bordered' id='tbl'> 
     <tbody> 
     <tr> 
      <td>Name</td> 
      <td>Heart Rate (Optional)</td> 
      <td>Intensity (Optional)</td> 
      <td>Description</td> 
     </tr> 

     @foreach($workoutcategories as $workoutcategory) 
      <tr> 
       <td>{!! Form::text('name[]', $workoutcategory->name, ['class' => 'form-control', 'size' => '15']) !!}</td> 
       <td>{!! Form::text('heartrate[]', $workoutcategory->heartrate, ['class' => 'form-control', 'size' => '1']) !!}</td> 
       <td>{!! Form::text('intensity[]', $workoutcategory->intensity, ['class' => 'form-control', 'size' => '1']) !!}</td> 
       <td>{!! Form::textarea('description[]', $workoutcategory->description,['class' => 'form-control', 'rows' => '4', 'cols'=>'100']) !!}{!! Form::hidden('id[]', $workoutcategory->id,['class' => 'form-control', 'rows' => '4', 'cols'=>'100']) !!}</td> 

      </tr> 
     @endforeach 

     <tr id="clone" style="display:none;"> 
      <td>{!! Form::text('name[]', null, ['class' => 'form-control', 'size' => '15']) !!}</td> 
      <td>{!! Form::text('heartrate[]', null, ['class' => 'form-control', 'size' => '1']) !!}</td> 
      <td>{!! Form::text('intensity[]', null, ['class' => 'form-control', 'size' => '1']) !!}</td> 
      <td>{!! Form::textarea('description[]', null,['class' => 'form-control', 'rows' => '4', 'cols'=>'100']) !!}{!! Form::hidden('id[]', null,['class' => 'form-control', 'rows' => '4', 'cols'=>'100']) !!}</td> 
     </tr> 


     </tbody> 
    </table> 

    <input type='button' id='Add' value='Add'> 

    <div class="form-group"> 
     {!! Form::submit('Update Workout Zones', ['class' => 'btn btn-primary form-control']) !!} 
     </form> 
    </div> 

外這裏是控制器。這很簡單。首先遍歷每個字段,確保名稱不是空的(我仍然有一些驗證問題,但有很多文件供我閱讀),然後updateOrCreate。這裏的邏輯檢查任何類別上的id是否與請求中的id匹配。如果是這樣,更新數組中的字段(名稱,HR,強度,desc)。否則,創建一個新記錄(Laravel自動創建,不需要調用保存)。祝你有個人需要解決這個問題!

public function update(WorkoutCategory $workoutCategory, WorkoutCategoryRequest $request) { 

     foreach($request->input('name') as $key => $value) { 
      if(!empty($request->input('name.' . $key))) { 
      $workoutCategory->updateOrCreate(['id' => $request->input('id.' . $key)], ['name' => $request->input('name.' . $key), 'heartrate' => $request->input('heartrate.' . $key), 'intensity' => $request->input('intensity.' . $key), 'description' => $request->input('description.' . $key)]); 
      } 
     } 

     flash()->overlay('Your workout zones have been updated!', 'Good Job!'); 
     return redirect('workoutcategories') 
      ; 
    }