2017-10-20 70 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不使用{{}},因为它只编译变量而不是函数 –

相关问题