我想同时在三个不同的表中添加插入数据。但是一旦将数据插入到第一个表中,我需要获取该id并继续使用该数据在另外两个表中插入数据。这些是申请人表,语言表(每个申请人可能知道多种语言)和技能表(也是一个或多个)。
以下是我一直在尝试的代码:
型号:
class Apply extends Eloquent {
protected $table = 'application';
protected $fillable = ['user_id', 'gender', 'dob', 'ic', 'marriage_status', 'phone', 'city', 'street', 'course', 'university',
'academic_degree', 'uni_start_date', 'uni_end_date', 'addinfo', 'working_company', 'position', 'start_date', 'end_date', 'reason'];
public function lang() {
return $this->hasMany('Lang');
}
}
class Lang extends Eloquent
{
protected $table = 'language';
public function apply(){
return $this->belongsTo('Apply');
}
}
class Skills extends Eloquent
{
protected $table = 'skills';
public function apply(){
return $this->belongsTo('Apply');
}
}
控制器:
public function createcv() {
$input = Input::all();
$rules = array('ic' => 'required',
'phone' => 'required',
'course' => 'required');
$v = Validator::make($input, $rules);
if ($v->passes()) {
$apply = Apply::create(array('user_id' => Auth::user()->id, 'gender' => $input['gender'],
'dob' => $input['dob'], 'ic' => $input['ic'], 'marriage_status' => $input['mstatus'],
'phone' => $input['phone'], 'city' => $input['city'], 'street' => $input['street'],
'course' => $input['course'], 'university' => $input['institution'],
'academic_degree' => $input['adegree'], 'uni_start_date' => $input['uni-startdate'],
'uni_end_date' => $input['uni-enddate'], 'addinfo' => $input['addinfo'],
'working_company' => $input['jobinstitution'], 'position' => $input['jobposition'],
'start_date' => $input['startdate'], 'end_date' => $input['enddate'], 'reason' => $input['reason']));
$apply->save();
$app_id = $apply->id;
$lang = Lang::create(array('app_id' => $app_id, 'lang' => $input['lang1'],
'reading' => $input['read1'], 'writing' => $input['write1'],
'speaking' => $input['speak1'], 'institution' => $input['inst1']
));
$lang->save();
$skills = Skills::create(array('app_id' => $app_id, 'prog_name' => $input['prog1'],
'level' => $input['level1'], 'institution' => $input['instprog1']));
$skills->save();
return Redirect::to('home');
} else {
return Redirect::to('create-cv')->withInput()->withErrors($v);
}
}
答案 0 :(得分:1)
如果您需要记录ID来存储其他内容,则不能同时保存这些内容,因为当您保存数据时,您可以检索ID。
当您调用模型的save
方法时,您可以检索数据库中记录的id
,如果您的数据具有相同的键($ key => $ value),那么您可以使用create方法的列的名称。
:
您也可以使用create方法将新模型保存在一行中。插入的模型实例将从方法返回给您。但是,在执行此操作之前,您需要在模型上指定可填充或受保护的属性,因为所有Eloquent模型都可以防止批量分配。
使用模型创建方法
// Create a new user in the database... $user = User::create(array('name' => 'John')); // Retrieve the user by the attributes, or create it if it doesn't exist... $user = User::firstOrCreate(array('name' => 'John')); // Retrieve the user by the attributes, or instantiate a new instance... $user = User::firstOrNew(array('name' => 'John'));
所以你的代码看起来像这样:
$apply = new Apply();
...
$apply->save();
$id_of_your_apply = $apply->id;
答案 1 :(得分:0)
https://laravel.com/docs/4.2/eloquent#insert-update-delete
public function createcv(){
$input = Input::all();
$rules = array('ic' => 'required',
'phone' => 'required',
'course' => 'required');
$v = Validator::make($input, $rules);
if ($v->passes()) {
$apply = Apply::create(array('user_id' => Auth::user()->id,'gender' => $input['gender'], 'dob' => $input['dob'], 'ic' => $input['ic'], 'marriage_status' => $input['mstatus'], 'phone' => $input['phone'],'city' => $input['city'],'street'=> $input['street'], 'course' => $input['course'], 'university' => $input['institution'], 'academic_degree' => $input['adegree'],'uni_start_date' => $input['uni-startdate'], 'uni_end_date' => $input['uni-enddate'],'addinfo' => $input['addinfo'], 'working_company' => $input['jobinstitution'],'position' => $input['jobposition'], 'start_date' => $input['startdate'],'end_date' => $input['enddate'], 'reason' => $input['reason']));
$apply->save();
**$app_id = $apply->id;**
$lang = Lang::create(array('app_id' => $app_id,'lang' => $input['lang1'],
'reading' => $input['read1'], 'writing' => $input['write1'],
'speaking' => $input['speak1'],'institution' => $input['inst1']
));
$lang->save();
$skills = Skills::create(array('app_id' => $app_id,'prog_name' => $input['prog1'], 'level' => $input['level1'], 'institution' => $input['inst1']));
$skills->save();
return Redirect::to('home');
}
else {
return Redirect::to('create-cv')->withInput()->withErrors($v);
}
}
答案 2 :(得分:0)
在研究了代码之后,我发现我使用了Laravel关键字" Lang"哪个laravel不允许你使用。还有很多其他关键字: 郎,事件等不能使用。 此代码适用于将数据添加到多个表(laravel 4)