我在解决这个问题时遇到了一些困难,当我试图更新我的数据时,我总是收到这个错误: implode():传递了无效的参数。
信息:我正在尝试更新这3个表,用户,学校和爱好。其中用户可以有很多学校,用户可以有很多爱好。
问题:当我尝试更新用户或学校数据时,我会得到一个错误"implode():传递了无效的参数“,但当我尝试更新爱好数据时,它是成功的。
但是,如果我删除了业余爱好数据的功能,我就可以编辑用户或学校数据。
我想要的是能够更新我所有的数据
HomeController:
//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">发布于 2017-10-20 14:30:59
请查一下这条线路
$test2['hobby] = implode(' , ', $request->hobby);发布于 2017-10-20 14:25:38
在方法中:
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);https://stackoverflow.com/questions/46843298
复制相似问题