Keep getting error: implode(): Invalid arguments passed when updating data

Keep getting error: implode(): Invalid arguments passed when updating data

每当我尝试将某些内容更新到我的数据中时,我总是收到此错误。上次并没有发生太多,但是在放置新的更新功能之后,错误不断弹出并且由于某种原因它指向我输入的新功能,即使我正在更新旧功能。

控制器:

//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

我更新爱好数据没问题,但错误一直指向爱好内爆部分。

我可以只对内爆函数使用一次更新吗?提前致谢

edit2.blade.php(爱好编辑页面,如图是一个数组)

  <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">

您收到错误是因为 $request->computer_game 不是数组。这可以通过转储变量并终止脚本来验证。

var_dump($request->computer_game); die;

首先 var_dump(); 是做什么的?给那个面向对象的数组。请分享然后我们会尽力回答。

然后要停止多次加载,您可以创建一个私有变量($loaded),默认情况下它将为 falseprivate static $loaded = false; 然后在函数中开始任何代码之前使用 if 语句来检测 loaded 是否为 true

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

这对你有帮助!请postvar_dump!

只需简单地尝试以下操作:

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

这会将 $request->computer_game 转换为数组(如果尚未转换)。