我在解决这个问题时遇到了一些困难,我继续收到此错误: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">
答案 0 :(得分:1)
请检查此行
$test2['hobby] = implode(' , ', $request->hobby);
答案 1 :(得分:0)
方法:
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);