向多个表添加数据 (laravel 4)
Adding data to multiple tables (laravel 4)
我想同时在三个不同的table中添加插入数据。但是在第一个 table 中插入数据后,我需要获取该 ID 并继续使用它在其他两个 table 中插入数据。这些是申请人table、语言table(每个申请人可能会不止一种语言)和技能table(也是一种或多种)。
下面是我一直在尝试的代码:
型号:
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);
}
}
如果你需要一个record id来存储其他的,这些不能同时保存,因为你保存数据的时候就是你可以检索id的时候。
当您调用模型的 save
方法时,您可以检索数据库中记录的 id
,如果您的数据具有相同的键($key => $value)您可以使用创建方法的列的名称。
来自 Laravel 页:
You may also use the create method to save a new model in a single line. The inserted model instance will be returned to you from the method. However, before doing so, you will need to specify either a fillable or guarded attribute on the model, as all Eloquent models protect against mass-assignment.
Using The Model Create Method
// 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;
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);
}
}
研究代码后,我发现我使用了 Laravel 关键字 "Lang",laravel 不允许您使用该关键字。还有许多其他关键字,例如:
不能使用的 Lang、Events 等。
此代码适用于将数据添加到多个表 (laravel 4)
我想同时在三个不同的table中添加插入数据。但是在第一个 table 中插入数据后,我需要获取该 ID 并继续使用它在其他两个 table 中插入数据。这些是申请人table、语言table(每个申请人可能会不止一种语言)和技能table(也是一种或多种)。
下面是我一直在尝试的代码:
型号:
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);
}
}
如果你需要一个record id来存储其他的,这些不能同时保存,因为你保存数据的时候就是你可以检索id的时候。
当您调用模型的 save
方法时,您可以检索数据库中记录的 id
,如果您的数据具有相同的键($key => $value)您可以使用创建方法的列的名称。
来自 Laravel 页:
You may also use the create method to save a new model in a single line. The inserted model instance will be returned to you from the method. However, before doing so, you will need to specify either a fillable or guarded attribute on the model, as all Eloquent models protect against mass-assignment.
Using The Model Create Method
// 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;
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);
}
}
研究代码后,我发现我使用了 Laravel 关键字 "Lang",laravel 不允许您使用该关键字。还有许多其他关键字,例如: 不能使用的 Lang、Events 等。 此代码适用于将数据添加到多个表 (laravel 4)