【问题标题】:implode(): Invalid arguments passed laravelimplode():通过 laravel 传递的参数无效
【发布时间】:2017-10-20 06:14:58
【问题描述】:

我在解决这个问题时遇到了一些困难,我一直收到这个错误:implode(): Invalid arguments passed when I try to update my data.

信息:我正在尝试更新这 3 个表,用户、学校和爱好。用户可以有很多学校,用户可以有很多爱好。

问题:当我尝试更新用户或学校数据时,我会得到一个错误“implode(): Invalid arguments passed”,但是当我尝试更新爱好数据时,它是成功的。

但如果我要删除爱好数据的功能,我可以同时编辑用户或学校数据。

我想要的是能够更新我的所有数据

家庭控制器:

//update for personal_info
    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');
    }

test.blade.php(显示所有用户以及与他相关的其他信息)

<h1>User Information</h1>
@foreach ($data as $object)
    <b>Name: </b>{{ $object->Name }}<br><br>
<a href="{{ url('/user/show/'.$object->id.'/edit') }}">Edit</a>
<h1>School information</h1>

    @foreach ($object->schools as $object2)
    <b>School: </b>{{ $object2->School }}<br><br>
    <b>Start Date: </b>{{ $object2->SDate }}<br><br>
    <b>End Date: </b>{{ $object2->EDate }}<br><br>
    <a href="{{ url('/user/show/'.$object2->id.'/edit1') }}">Edit</a>

<h1>Hobbies</h1>
 @foreach ($object->hobbys as $object3)
    <b>hobby: </b>{{ $object3->computer_game }}<br><br>
    <b>sport: </b>{{ $object3->reading_book }}<br><br>
    <a href="{{ url('/user/show/'.$object3->id.'/edit2') }}">Edit</a>

edit.blade.php(用户编辑页面)

<form class="form-horizontal" method="post" action="{{ url('/user/show/'.$object->id) }}">
  {{ method_field('PUT')  }}
  {{ csrf_field() }}

  <div class="form-group">
    <label class="col-md-2">Name of user:</label>
    <div class="col-md-6">
      <input type="text" name="Name" value="{{ $object->Name }}" class="form-control">
  </div>
  <div class="form-group">
    <div class="col-md-6-offset-2">
      <input type="submit" class="btn btn-primary" value="Save">
    </div>

edit1.blade.php(用于学校,类似于edit2,使用html表格,可以添加行添加就读的学校用户数)

          <form class="form-horizontal" method="post" action="{{ url('/user/show/'.$object2->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">
              Name of School/University/Professional Institute:
            </th>
            <th class="text-center">
              Start Date:
            </th>
            <th class="text-center">
              End Date:
            </th>
          </tr>
        </thead>
        <tbody>
          <tr id='addr0'>
            <td>
            1
            </td>
            <td>
            <input type="text" name='School[]' class="form-control"/>
            </td>
            <td>
            <input type="date" name='SDate[]' class="form-control"/>
            </td>
            <td>
            <input type="date" name='EDate[]' class="form-control"/>
            </td>
          </tr>
                    <tr id='addr1'></tr>
        </tbody>
      </table>
          <div class="form-group">
            <div class="col-md-6-offset-2">
              <input type="submit" class="btn btn-primary" value="Save">
            </div>
          </div>

edit2.blade.php(用户可以在其中添加行以添加有关他的爱好的更多信息的html表)

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

【问题讨论】:

  • 究竟是什么错误?
  • 每当我尝试编辑我的数据时,我都会收到此错误“implode(): Invalid arguments passed”
  • 你能把错误粘贴过来吗...
  • 我已经编辑了我的问题
  • 我没有看到任何错误日志

标签: php laravel implode


【解决方案1】:

请检查此行

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

【讨论】:

  • 我已经更新了还是一样的错误,是我刚才输入的时候打错了
  • 确保第二个参数是一个数组。
  • 我用过 $test2= array();确保它变成一个数组
  • 我可以成功更新,但personal_infos和user_infos会得到错误“implode(): Invalid arguments passed”,如果我要删除user_info1s,personal_infos和user_infos可以更新
  • $request->School 是一个数组吗?
【解决方案2】:

在方法中:

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

使用这个

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

而不是这个

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

【讨论】:

    猜你喜欢
    • 2014-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多