Laravel 4 - 多对多 - 数据透视表不更新

时间:2014-06-03 17:57:08

标签: laravel laravel-4 table-relationships

向数据库添加新作业时没有明显错误。

我的行业工作进入了工作表,但我在连接表中与部门的关系无处可去。我无法看到我出错的地方

加入表

division_industryjob division_id industryjob_id

ID division_name

industryjobs ID JOB_TITLE

模型

Division.php

<?php 

class Division extends \Eloquent {

    protected $table = 'divisions';

    /**
     * Industry Jobs relationship
     */

    public function industryjobs()
    {
        return $this->belongsToMany('IndustryJob');
    }


}

IndustryJob.php

<?php 

class IndustryJob extends \Eloquent {

    protected $table = 'industryjobs';


     public function divisions()
    {
        return $this->belongsToMany('Division');
    }


}

路线

Route::get('industry-jobs/add', 'AdminController@getCreateIndustryJob');
Route::post('industry-jobs/add', 'AdminController@postCreateIndustryJob')

CONTROLLER

// Create Industry - Get (empty form - new entry)
    public function getCreateIndustryJob()
    {

        View::share('page_title', 'Create a new Industry Job Role');
        View::share('sub_page_title', 'Ex: Mechanical Technician');
        return View::make('admin/industry-jobs/create');
    }

    // Create Industry - Post 
    public function postCreateIndustryJob()
    {

        //validate user input
        $rules = array(
                'job_title' => 'Required|Min:3|Max:80'
        );

        $validation = Validator::make(Input::all(), $rules);

        If ($validation->fails())
        {
            return Redirect::to('/admin/industry-jobs/add')->withErrors($validation);
        } else {


        $industryjob = new IndustryJob;

        $industryjob->job_title = Input::get('job_title');
        $industryjob->job_description    = Input::get('job_description');
        $industryjob->job_qualifications    = Input::get('job_qualifications');

        if (isset($input['divisions'])) {
                foreach ($input['divisions'] as $divId) {
                    $div = Division::find($divId);
                    $industryjob->divisions()->save($div);
                }
            }


        $industryjob->save();

        return Redirect::to('/admin/industry-jobs')->with('message', 'Industry Job created successfully');

        }
    }

FORM

<form class="form-horizontal" method="post" autocomplete="off">

         <!-- Industry Job Title  -->
                <div class="form-group">
                    <label class="col-md-2 control-label" for="industry_name">Industry Job Title (*)</label>

                <div class="col-md-10">
                        <input class="form-control" type="text" name="job_title" id="job_title" value="" />
                    </div>
                </div>
         <!-- ./ Industry Job Title -->


         <!-- Industry Type  -->
           <div class="form-group">
           <label class="col-md-2 control-label" for="body">Related Division</label>
            <div class="col-md-10">
            <select name="divisions[]" id="divisions" size="6" class="form-control" multiple>
                @foreach (Division::all() as $division)
                    <option value="{{ $division->id }}" >{{ $division->division_name }}</option>
                @endforeach
            </select>
            </div>
        </div>
        <!-- ./ Industry Type -->



        <!-- Form Actions -->
        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <button type="reset" class="btn btn-default">Reset</button>
                <button type="submit" class="btn btn-success">Create Job</button>
            </div>
        </div>
        <!-- ./ form actions -->

    </form>

1 个答案:

答案 0 :(得分:3)

之后您正在保存Parent模型,因此无效。在保存Parent模型之前保存Child模型,所以它应该是这样的:

$industryjob = new IndustryJob;
$industryjob->job_title = Input::get('job_title');
$industryjob->job_description    = Input::get('job_description');
$industryjob->job_qualifications    = Input::get('job_qualifications');
$industryjob->save();

然后使用sync保存相关模型,因为它是many-to-many关系,并且已在数据库中创建并提供了相关模型:

if (isset($input['divisions'])) {
    // Pass the array of ids to sync method
    $industryjob->divisions()->sync($input['divisions']);
}

如果你使用foreach循环,那么你可以使用这样的东西;

foreach ($input['divisions'] as $divId) {
    $industryjob->divisions()->attach($divId);
}

详细了解Laravel网站上的inserting related models