我正在尝试将我的数据更新到我的数据库,其关系是我有很多学校,所以当我尝试保存在我的数据库中时,我希望得到这样的结果: [![在此处输入图像说明] [1]] [1]
但我只知道如何使用implode函数来实现它,我只能这样做:
我尝试过这样做,但它不起作用:
public function update1(Request $request, $user_id){
$rows = qualification::where('user_id', $user_id)->get();
foreach ($rows as $row){
switch ($row['meta_key']){
case 'school_name':
$row['meta_value'] = $request->input('School');
break;
case 'start_date':
$row['meta_value'] = $request->input('SDate');
break;
case 'end_date':
$row['meta_value'] = $request->input('EDate');
break;
case 'qualification_list':
$row['meta_value'] = $request->input('qualification');
break;
}
$row->save();
}
return redirect('/home');
}
当我尝试使用该代码时,它会给我这个错误,Array to string conversion 个人信息模型:
class PersonalInfo extends Eloquent
{
use SoftDeletes;
protected $table = 'personal_infos';
protected $primaryKey = 'id';
protected $dates = ['deleted_at'];
public function userQualifications()
{
return $this->hasMany('App\Qualification','user_id');
}
资格模型:
class Qualification extends Model
{
protected $table = "qualifications";
public function PersonalInfos() {
return $this->belongsTo('App\PersonalInfo');
}
}
答案 0 :(得分:0)
假设您在用户模型中正确设置了您的关系,您可以尝试这样的事情(未经测试):
public function update1(Request $request, $user_id){
// get the user
$user = PersonalInfo::find($user_id);
// create the new array of meta data
$data = array();
// loop through the inputs array count
for($i=0; $i < count($request->input('School')); $i++) {
$data[] = [
'meta_key' => 'school_name',
'meta_value' => $request->input('School')[$i]
];
$data[] = [
'meta_key' => 'start_date',
'meta_value' => $request->input('SDate')[$i]
];
$data[] = [
'meta_key' => 'end_date',
'meta_value' => $request->input('EDate')[$i]
];
$data[] = [
'meta_key' => 'qualification_list',
'meta_value' => $request->input('qualification')[$i]
];
}
// create the relationships
$user->userQualifications()->createMany($data);
return redirect('/home');
}