Laravel 4 - Many to Many - pivot table not updating

user3189734

I get no obvious errors when adding a new job to my database.

My industry job goes into the jobs table but the relationship with divisions in my join table goes nowhere. I just can't see where I'm going wrong

JOIN TABLE

division_industryjob division_id industryjob_id

divisions id division_name

industryjobs id job_title

MODELS

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


}

ROUTES

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>
The Alpha

You are saving the Parent model afterwards so it's not working. Save the Parent model before you save the Child Model, so it should be something like this:

$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();

Then save the related models using sync because it's many-to-many relationship and those related models are already created and available in the database:

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

If you use the foreach loop then you may use something like this;

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

Check more about inserting related models on Laravel website.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

laravel 4 Cannot Retrieve ALL results ? pivot table Many to Many

From Dev

Laravel Many-to-Many Pivot Table

From Dev

rails 4 many to many through pivot table

From Dev

Has many through with pivot table in Laravel4

From Dev

Pivot Table with many to many table

From Dev

Laravel Adding One-To-Many on a Pivot table

From Dev

Laravel Adding One-To-Many on a Pivot table

From Dev

Laravel Eloquent Many to Many relationship using a pivot table

From Dev

Laravel belongsToMany inserting '0' on pivot table ids in a many to many relationship?

From Dev

Laravel - Order by pivot value in Many to Many table relationship

From Dev

Laravel Eloquent many-to-many relationship: Use explicit pivot table

From Dev

Unable to sync conditional pivot table many to many relationship laravel

From Dev

Laravel L5.5 No access to pivot table in "Many to many" relationship

From Dev

Updating a many to many table with SQL

From Dev

SQL query Pivot Many to Many to Many Table

From Dev

Eloquent - Many to many where not found in the pivot table

From Dev

Laravel Removing Pivot data in many to many relationship

From Dev

Laravel 5.2 Many to many with custom pivot

From Dev

laravel many to many with third table

From Dev

Laravel 4: many to many (insertion)

From Dev

Laravel 4 many to many relationships

From Dev

Laravel 4 whereIn and many to many

From Dev

Laravel 4: many to many (insertion)

From Dev

Creating one to many relationship with pivot table of many to many relationship

From Dev

Creating one to many relationship with pivot table of many to many relationship

From Dev

Laravel 4.2 Many-to-many relationship : Can't read from pivot table

From Dev

Laravel Eloquent ORM - Many to Many Delete Pivot Table Values left over

From Dev

Should eloquent's many-to-many pivot table enforce uniqueness accross the two id columns? Laravel 5

From Dev

Rails 4 oracle enhanced adapter many to many propagating data to the pivot table

Related Related

  1. 1

    laravel 4 Cannot Retrieve ALL results ? pivot table Many to Many

  2. 2

    Laravel Many-to-Many Pivot Table

  3. 3

    rails 4 many to many through pivot table

  4. 4

    Has many through with pivot table in Laravel4

  5. 5

    Pivot Table with many to many table

  6. 6

    Laravel Adding One-To-Many on a Pivot table

  7. 7

    Laravel Adding One-To-Many on a Pivot table

  8. 8

    Laravel Eloquent Many to Many relationship using a pivot table

  9. 9

    Laravel belongsToMany inserting '0' on pivot table ids in a many to many relationship?

  10. 10

    Laravel - Order by pivot value in Many to Many table relationship

  11. 11

    Laravel Eloquent many-to-many relationship: Use explicit pivot table

  12. 12

    Unable to sync conditional pivot table many to many relationship laravel

  13. 13

    Laravel L5.5 No access to pivot table in "Many to many" relationship

  14. 14

    Updating a many to many table with SQL

  15. 15

    SQL query Pivot Many to Many to Many Table

  16. 16

    Eloquent - Many to many where not found in the pivot table

  17. 17

    Laravel Removing Pivot data in many to many relationship

  18. 18

    Laravel 5.2 Many to many with custom pivot

  19. 19

    laravel many to many with third table

  20. 20

    Laravel 4: many to many (insertion)

  21. 21

    Laravel 4 many to many relationships

  22. 22

    Laravel 4 whereIn and many to many

  23. 23

    Laravel 4: many to many (insertion)

  24. 24

    Creating one to many relationship with pivot table of many to many relationship

  25. 25

    Creating one to many relationship with pivot table of many to many relationship

  26. 26

    Laravel 4.2 Many-to-many relationship : Can't read from pivot table

  27. 27

    Laravel Eloquent ORM - Many to Many Delete Pivot Table Values left over

  28. 28

    Should eloquent's many-to-many pivot table enforce uniqueness accross the two id columns? Laravel 5

  29. 29

    Rails 4 oracle enhanced adapter many to many propagating data to the pivot table

HotTag

Archive