如何从多选表格向2表插入数据

时间:2018-11-05 03:28:34

标签: javascript php laravel-5

我想将“表单”中的数据插入表:project和table:projects_departments

这是我的表格 enter image description here

表:部门enter image description here

表:projects_departments enter image description here

表:项目 enter image description here

我的表格

 {!! Form::open(['route' => 'project.store']) !!}
     {{ csrf_field() }}
           <div class="row" name="add_name" id="add_name">
                 <div class="col-md-12" >
                     <div class="form-group department-select-container">            
                       {!! Form::label('departments_id', 'Department Name :'); !!}
           <?= Form::select('departments_id', App\Department::all()->pluck('department_name', 'id'), null, [
                                'class' => 'form-control', 'placeholder' => '-- Please Select --']); ?>

       <a href="#" class="btn btn-sm btn-info btn-add-more-department" onclick="createNew()">ADD</a>
                             <div id="mydiv"></div>
                        </div>
                    </div>
                </div>

ProjectController 功能库

public function store(Request $request)
{
    $this->validate($request, [
        'departments_id'    => 'required',

        'years_id'          => 'required',
        'strategys_id'      => 'required',
        'objectives_id'     => 'required',
        'tactics_id'        => 'required',
        'projects_name'     => 'required',
        'target_group'      => 'required',
        'kpis_id'           => 'required',
        'kpiunits_id'       => 'required',
        'level_1'           => 'required',
        'level_2'           => 'required',
        'level_3'           => 'required',
        'level_4'           => 'required',
        'level_5'           => 'required',
        'level_1_detail'    => 'required',
        'level_2_detail'    => 'required',
        'level_3_detail'    => 'required',
        'level_4_detail'    => 'required',
        'level_5_detail'    => 'required',
    ]);


 Project::create($request->all());
    return redirect()->route('project.index')->with('success','recorded successfully');
}

这是Model:Project

protected $table = 'projects';
protected $fillable = [
    'departments_id', 
    'years_id', 
    'strategys_id', 
    'objectives_id',
    'tactics_id',
    'projects_name',
    'target_group',
    'kpis_id',
    'kpiunits_id',
    'level_1',
    'level_2',
    'level_3',
    'level_4',
    'level_5',
    'level_1_detail',
    'level_2_detail',
    'level_3_detail',
    'level_4_detail',
    'level_5_detail',
    ];

public function years() {
    return $this->hasOne('App\Year', 'id', 'years_id');
}

public function strategys() {
    return $this->hasOne('App\Strategy', 'id', 'strategys_id');
}

public function objectives() {
    return $this->hasOne('App\Objective', 'id', 'objectives_id');
}
public function departments() {
    return $this->hasOne('App\Department', 'id', 'departments_id');
}

public function tactics() {
    return $this->hasOne('App\Tactic', 'id', 'tactics_id');
}

public function kpis() {
    return $this->hasOne('App\Kpi', 'id', 'kpis_id');
}
public function kpiunits() {
    return $this->hasOne('App\Kpiunit', 'id', 'kpiunits_id');
}



public function projects_departments() {
    return $this->belongsTo(Project_department::class, 'departments_id'); 
}

这是Model:Project_department

 protected $table = 'projects_departments';
 protected $fillable = [
    'projects_id', 
    'departments_id', 
    ];

public function projects()
{
    return $this->belongsToMany('App\Project');
}

0 个答案:

没有答案