Laravel Many to Many relation

aegyed

I getting started with Eloquent, and I've ran into a problem. I trying to setup a many to many relation schema + model.

Here is the code:

routes.php snippet:
$user1 = User::findOrFail(1);
$user2 = User::where('username', '=', 'TestUser')->get();
// we make $user1 to follow $user2
$user1->followedBy()->save($user2);

User model snippet:

public function followedBy() {
    return $this->belongsToMany('User', 'user_follows', 'user_id', 'follow_id');
}

public function following() {
    return $this->belongsToMany('User', 'user_follows', 'follow_id', 'user_id' );
}

DB Schema snippet:

Schema::create('user_follows', function(Blueprint $table)
        {
            $table->increments('id');
            $table->integer('user_id');
            $table->integer('follow_id');
            $table->timestamps();
        });

The error I get when I visit the routes:

Argument 1 passed to Illuminate\Database\Eloquent\Relations\BelongsToMany::save() must be an instance of Illuminate\Database\Eloquent\Model, instance of Illuminate\Database\Eloquent\Collection given, called in /Applications/MAMP/htdocs/Laravel/proj2/app/routes.php on line 84 and defined 
Jarek Tkaczyk

The error says it all:

$user2 = User::where('username', '=', 'TestUser')->get();
// returns Collection

You need this instead:

$user2 = User::where('username', '=', 'TestUser')->first();
// returns single Model

And by the way:

// we make $user1 to follow $user2
$user1->followedBy()->save($user2);

it's $user2 to follow $user1 and use this:

$user1->followedBy()->attach($user2);

For save is saving the $user2 model first, what is redundant here. It's used in such cases:

$user2 = new User;
// assign some properties
...
$user1->followedBy()->save($user2);

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 5.1 many to many relation

From Dev

Laravel Many-to-Many Relation

From Dev

Search in many to many relation Laravel

From Dev

Laravel many relation insert

From Dev

Laravel one to many relation

From Dev

Laravel ORM many to many relation with conditions on keys

From Dev

save many to many relation in Laravel elequent 5?

From Dev

Laravel get data from many to many relation

From Dev

Laravel: ordering a many-many relation

From Dev

Laravel - Many to Many Polymorphic Relation with Extra Fields

From Dev

Laravel Repository pattern and many to many relation

From Dev

save many to many relation in Laravel elequent 5?

From Dev

laravel Many To Many relationship with inner relation

From Dev

Laravel Has Many Through relation

From Dev

Many to Many Eloquent Relation

From Dev

Working with Many to Many relation

From Dev

Select for many to many relation

From Dev

Select on many to many relation

From Dev

Pull out the specific data when the relation ship is many to many in laravel

From Dev

Laravel: Get pivot data for specific many to many relation

From Dev

Laravel seeding my tables(many to many relation) fails

From Dev

Laravel 4.1 WhereIn w/ Many to Many Relation and Conditionals?

From Dev

How to use many to many polymorphic relation in Laravel 5.2

From Dev

Laravel 4.2 many to many relation, use something else then the default ID

From Dev

Laravel - Eloquent relation - many-to-many - get intermediate table columns

From Dev

Laravel / Eloquent - Many to many pivot model with morphToMany relation

From Dev

laravel 5.2 many to many relation retrieve data with intermediate table

From Dev

Laravel Model Relation Where Many-to-Many Exists

From Dev

Laravel One-to-Many relation - Insertion

Related Related

  1. 1

    laravel 5.1 many to many relation

  2. 2

    Laravel Many-to-Many Relation

  3. 3

    Search in many to many relation Laravel

  4. 4

    Laravel many relation insert

  5. 5

    Laravel one to many relation

  6. 6

    Laravel ORM many to many relation with conditions on keys

  7. 7

    save many to many relation in Laravel elequent 5?

  8. 8

    Laravel get data from many to many relation

  9. 9

    Laravel: ordering a many-many relation

  10. 10

    Laravel - Many to Many Polymorphic Relation with Extra Fields

  11. 11

    Laravel Repository pattern and many to many relation

  12. 12

    save many to many relation in Laravel elequent 5?

  13. 13

    laravel Many To Many relationship with inner relation

  14. 14

    Laravel Has Many Through relation

  15. 15

    Many to Many Eloquent Relation

  16. 16

    Working with Many to Many relation

  17. 17

    Select for many to many relation

  18. 18

    Select on many to many relation

  19. 19

    Pull out the specific data when the relation ship is many to many in laravel

  20. 20

    Laravel: Get pivot data for specific many to many relation

  21. 21

    Laravel seeding my tables(many to many relation) fails

  22. 22

    Laravel 4.1 WhereIn w/ Many to Many Relation and Conditionals?

  23. 23

    How to use many to many polymorphic relation in Laravel 5.2

  24. 24

    Laravel 4.2 many to many relation, use something else then the default ID

  25. 25

    Laravel - Eloquent relation - many-to-many - get intermediate table columns

  26. 26

    Laravel / Eloquent - Many to many pivot model with morphToMany relation

  27. 27

    laravel 5.2 many to many relation retrieve data with intermediate table

  28. 28

    Laravel Model Relation Where Many-to-Many Exists

  29. 29

    Laravel One-to-Many relation - Insertion

HotTag

Archive