2017-10-20 68 views
1

每當我嘗試將某些內容更新到我的數據中時,我總是收到此錯誤。它最後一次沒有發生很多事情,但在放入新的更新函數後,錯誤不斷出現,出於某種原因,它指向我已經輸入的新函數,即使我正在更新舊函數。不斷收到錯誤:implode():更新數據時傳遞的參數無效

控制器:

//update for user 
    public function edit($id){ 
     $object = user::find($id); 

     return view('edit', compact('object')); 

    } 

    public function update(Request $request, $id){ 
     $object = user::find($id); 
     $object->Name = $request->input('Name'); 
     $object->update(); 

     return redirect('/home'); 
    } 


    //update for Schools table 
    public function edit1($id){ 
     $object2 = school::find($id); 
     return view('edit1', compact('object2')); 

    } 
    public function update1(Request $request, $id){ 
     $object2 = school::find($id); 
     $test = array(); 
    $test['School'] = implode(' , ', $request->School); 
    $test['SDate'] = implode(' , ', $request->SDate); 
    $test['EDate'] = implode(' , ', $request->EDate); 
     $object2->update($test); 
     return redirect('/home'); 
    } 

    //error start here after putting this whole thing in. (I tried putting it into another separate controller but the error still continues) 
      public function edit2($id){ 
     $object3 = hobby::find($id); 
     return view('edit2', compact('object3')); 

    } 
    public function update2(Request $request, $id){ 
     $object3 = hobby::find($id); 
    $test2 = array(); 
    $test2['computer_game'] = implode(' , ', $request->computer_game); 
    $test2['reading_book'] = implode(' , ', $request->reading_book); 
     $object3->update($test2); 
     return redirect('/home'); 
    } 

錯誤是突出這個部分,即使當我嘗試更新的用戶或學校數據

$test2['computer_game'] = implode(' , ', $request->computer_game); 

它說

:implode(): Invalid arguments passed

我沒有問題更新愛好數據,但它的錯誤一直指向愛好implode部分。

是否有可能我只能在implode函數上使用update一次?在此先感謝

edit2.blade.php(.584編輯頁面,如圖所示它是一個數組)

<form class="form-horizontal" method="post" action="{{ url('/user/show/'.$object3->id) }}"> 
       {{ method_field('PUT') }} 
       {{ csrf_field() }} 
    <table class="table table-bordered table-hover" id="tab_logic"> 
      <thead> 
       <tr > 
          <th class="text-center"> 
        # 
       </th> 
       <th class="text-center"> 
        Sports: 
       </th> 
       <th class="text-center"> 
        Books read: 
       </th> 
           </tr> 
      </thead> 
      <tbody> 
       <tr id='addr0'> 
       <td> 
       1 
       </td> 
       <td> 
       <input type="text" name='computer_game[]' class="form-control"/> 
       </td> 
       <td> 
       <input type="text" name='reading_book[]' class="form-control"/> 
       </td> 
           </tr> 
         <tr id='addr1'></tr> 
      </tbody> 
      </table> 
<input type="submit" class="btn btn-primary" value="Save"> 
+0

'var_dump($ request-> computer_game)'產生了什麼? – mulquin

+0

你能告訴我們var_dump($ request-> computer_game)的返回值嗎? – Maraboc

+0

@Maraboc它什麼也沒有 – blastme

回答

1

只是簡單的請嘗試以下操作:

$test2['computer_game'] = implode(' , ', (array)$request->computer_game); 

這將在轉換$request->computer_game一個數組,如果它是不是已經。

+0

tks的答覆,我試過你的解決方案,它沒有得到任何錯誤,但沒有更新直到我的數據 – blastme

+0

y它返回我已經輸入的值,但沒有更新它 – blastme

+0

呃好的..我問什麼問題?對不起,我仍然不知道你發現什麼問題 – blastme

0

你所得到的錯誤,因爲$請求 - > computer_game不是數組。這可以通過轉儲變量並殺死腳本來驗證。

var_dump($request->computer_game); die; 
+0

tks的快速回復,我試着你的代碼沒有沒有任何錯誤,但沒有更新正在進行,實際上computer_game是一個數組,我將更新我的問題,向您展示編輯頁面 – blastme

0

首先var_dump()是什麼?給出那個面向對象的數組。我們會盡力回答。

然後停止多個負載可以使一個私人VAR($加載),默認情況下它會是假private static $loaded = false;然後befor任何代碼的功能使用開始,如果如果加載語句來檢測是真的

if(self::$loaded){ 
    return; 
} 
self::$loaded = true 

這會幫助你!請發佈var_dump!

+0

變量聲明應該在類的頂部。在任何代碼之前。 –

+0

所以你的var_dump不給任何東西;數組可能是空的。另一件事你應該注意到在編輯2不使用{{}},因爲它只編譯變量而不是函數 –

相關問題