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

ReactingToAngularVues

I have the following schema:

An objects table

    Schema::create('objects', function(Blueprint $table) {
        $table->increments('object_id');
    });

With a relation of:

public function tags() {
    return $this->belongsToMany('Tag', 'objects_tags_pivot', '?', '?');
}

A tags table:

    Schema::create('tags', function(Blueprint $table) {
        $table->increments('tag_id');
    });

With a relation of:

public function objects() {
    return $this->belongsToMany('Object', 'objects_tags_pivot', '?', '?');
}

And a pivot between them for the many to many relationship present:

    Schema::create('objects_tags_pivot', function(Blueprint $table) {
        $table->increments('object_tags_id');
        $table->integer('object_id')->unsigned();
        $table->integer('tag_id')->unsigned();
    });

My question is what to insert into the relationship parameters where I currently have question marks? The Laravel 4.2 documentation suggests that the third parameter is $foreignKey and the fourth is $localKey. This isn't very helpful.

Every attempt at matching object_id and tag_id up with these parameter, or leaving them blank, has resulted in one or more of the fields on the pivot table being 0. Not useful at all.

What order should I specify each parameter in?

Steve Bauman

Have you ever tried leaving them null? Laravel will already make foreign and local / other key assumptions based on the table name of the relation. For example, since the objects table model name is Object it will guess that the foreign key is named object_id. Also, since the tags model name is Tag, it will guess tag_id for the local / other key in a relationship.

If you want to fill them out manually, you need to insert the column names that indicate how the relationship is linked. For example:

public function tags()
{
     $this->belongsToMany('Tag', 'objects_tag_pivot', 'object_id', 'tag_id');
}

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 Many to Many Relationship ( hasMany or belongsToMany )

From Dev

Laravel Eloquent Many to Many relationship using a pivot table

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

inserting data into a table that has a many to many relationship

From Dev

Laravel Removing Pivot data in many to many relationship

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

inserting data into database laravel 5.6 relation belongstomany pivot table

From Dev

Laravel: belongsToMany() does not get fields in the many-to-many table

From Dev

Laravel 4 - Many to Many - pivot table not updating

From Dev

Laravel Many-to-Many Pivot Table

From Dev

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

From Dev

SQLAlchemy Inserting Data in a Many-to-Many Relationship with Association Table

From Dev

Inserting into many-to-many relationship table with a returned id

From Dev

How to query a pivot table data in MySQL (many to many relationship)

From Dev

Laravel many to many relationship, add more objects into "pivot"?

From Dev

Laravel - Many to Many relationship

From Dev

Many to many relationship in Laravel

From Dev

Many to Many relationship with Laravel

From Dev

Pivot Table with many to many table

From Dev

Laravel belongsToMany exclude pivot 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 4 Cannot Retrieve ALL results ? pivot table Many to Many

From Dev

Laravel - Inserting multiple items using one-to-many relationship

From Dev

Inserting a has many relationship tables at the same time in laravel

Related Related

  1. 1

    Laravel Many to Many Relationship ( hasMany or belongsToMany )

  2. 2

    Laravel Eloquent Many to Many relationship using a pivot table

  3. 3

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

  4. 4

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

  5. 5

    Unable to sync conditional pivot table many to many relationship laravel

  6. 6

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

  7. 7

    inserting data into a table that has a many to many relationship

  8. 8

    Laravel Removing Pivot data in many to many relationship

  9. 9

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

  10. 10

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

  11. 11

    inserting data into database laravel 5.6 relation belongstomany pivot table

  12. 12

    Laravel: belongsToMany() does not get fields in the many-to-many table

  13. 13

    Laravel 4 - Many to Many - pivot table not updating

  14. 14

    Laravel Many-to-Many Pivot Table

  15. 15

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

  16. 16

    SQLAlchemy Inserting Data in a Many-to-Many Relationship with Association Table

  17. 17

    Inserting into many-to-many relationship table with a returned id

  18. 18

    How to query a pivot table data in MySQL (many to many relationship)

  19. 19

    Laravel many to many relationship, add more objects into "pivot"?

  20. 20

    Laravel - Many to Many relationship

  21. 21

    Many to many relationship in Laravel

  22. 22

    Many to Many relationship with Laravel

  23. 23

    Pivot Table with many to many table

  24. 24

    Laravel belongsToMany exclude pivot table

  25. 25

    Laravel Adding One-To-Many on a Pivot table

  26. 26

    Laravel Adding One-To-Many on a Pivot table

  27. 27

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

  28. 28

    Laravel - Inserting multiple items using one-to-many relationship

  29. 29

    Inserting a has many relationship tables at the same time in laravel

HotTag

Archive