duancai7568 2017-07-18 17:04
浏览 40
已采纳

将数据添加到多个表(laravel 4)

I want to add insert data in three different tables at the same time. But once the data is inserted in the first table, I need to get that id and continue with that to insert data in the other two table. These are the applicant table, language table (each applicant may know more than one language) and skills table (also one or many).

Below is the code that I have been trying:

Models:

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');
}
}

Controller:

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);
    }
}
  • 写回答

3条回答 默认 最新

  • dongqiancui9194 2017-07-19 11:36
    关注

    After studying the code l found out that l have used a Laravel keyword "Lang" which laravel does not allow you to work with. There are many other keyword like: Lang, Events etc. which cant be used. This code works for Adding data to multiple tables (laravel 4)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 YOLOv5改进后的结构图
  • ¥15 全志v3s怎么设置高速时钟,使用的荔枝派zero开发板,串口2需要921600的波特率
  • ¥15 关于#单片机#的问题:Lora通讯模块hc-14电路图求内部原理图
  • ¥50 esp32 wroom 32e 芯片解锁
  • ¥15 bywave配置文件写入失败
  • ¥20 基于Simulink的ZPW2000轨道电路仿真
  • ¥15 pycharm找不到在环境装好的opencv-python
  • ¥15 在不同的执行界面调用同一个页面
  • ¥20 基于51单片机的数字频率计
  • ¥50 M3T长焦相机如何标定以及正射影像拼接问题