问题
I keep on getting this error whenever I try to update something into my data. It didn't happen a lot the last time but after putting the new update function, the error keep popping out and for some reason it points back to the new function that I had entered even though I am updating at the old function.
Controller:
//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');
}
Error is highlighting this part even though when I try to update user or school data
$test2['computer_game'] = implode(' , ', $request->computer_game);
And it says
:implode(): Invalid arguments passed
I have no issue updating the hobby data but it the error keep pointing it back to the hobby implode part.
Is it possible I could only use update once on an implode function? Thanks in advance
edit2.blade.php (edit page for hobby, as shown it is an array)
<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">
回答1:
Just simple try the following:
$test2['computer_game'] = implode(' , ', (array)$request->computer_game);
This will convert $request->computer_game
in to an array if it is not already.
回答2:
You are getting the error because $request->computer_game is not an array. This can be verified by dumping the variable and killing the script.
var_dump($request->computer_game); die;
回答3:
first of all what does var_dump(); gives to that object oriented array. plz share then we'll try to answer.
Then to stop multiple loading you can make a private var ($loaded) and by default it would be falseprivate static $loaded = false;
then befor any code starts in function use if statement to detect if loaded is true
if(self::$loaded){
return;
}
self::$loaded = true
this would help you! please post var_dump!
来源:https://stackoverflow.com/questions/46845065/keep-getting-error-implode-invalid-arguments-passed-when-updating-data