Laravel 5 | Many to Many Relationship not working

Shankar Thiyagaraajan

I'm new in laravel5. I use "Many to Many" Relationships to get all messages based on the given tag.

MESSAGE MODEL :

   function tags(){
      return $this->belongsToMany('App\tags')->withTimestamps();
    }

TAGS MODEL :

  public function messages() {
     return $this->belongsToMany('App\messages', "messages_tags",     "messages_id", "tags_id");
  }

MY INPUT :

   $tag = App\tags::where('name','public')->first();

($tag :)

 App\tags {#681
 id: "5",
 name: "Public",
 created_at: "2016-02-10 13:51:36",
 updated_at: "2016-02-10 08:21:36",
 }

I tried to get Messages with Tag.

 $tag->messages()->get();

MY OUTPUT :

 []

But i have messages with the Tag "Public".

What is Wrong with my code ?

The Alpha

In your Message model in the tags() method you should provide the messages_tags pivot table name as well (including "messages_id" and "tags_id") and to access the messages you should use:

$tag->messages;

Or you may use (Eagre loading):

$tag = App\tags::with('messages')->where('name','public')->first();

Then use:

$tag->messages;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Rails 5: Many-to-Many Relationship Not Working

From Dev

Laravel 5.2.29 Many to Many relationship not working

From Dev

Laravel 5 Eloquent count many to many relationship

From Dev

one to many relationship in Laravel5 is not working properly

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

Laravel - One to Many relationship is not working one way

From Dev

Laravel One to Many relationship not working - returns recursion

From Dev

Laravel - One to Many relationship is not working one way

From Dev

Laravel one to many model relationship not working

From Dev

Laravel 5 Eloquent Relationship - Has Many Though

From Dev

Laravel 5 Deleting a one-to-many relationship

From Dev

Laravel 5 one to many eloquent relationship

From Dev

Laravel 5 Eloquent Relationship - Has Many Though

From Dev

Building a dynamic query of a many-to-many relationship in Laravel 5

From Dev

Laravel 5 - access specific model on many to many relationship

From Dev

Empty data returning from many to many relationship laravel 5

From Dev

Laravel 5 - Dedicated Query string filtering on many-to-many Relationship

From Dev

Laravel 6 many to many relationship

From Dev

Pagination with many to many relationship in laravel

From Dev

Laravel many to many relationship error

From Dev

Laravel many to many relationship with conditions

From Dev

Laravel ORM many to many relationship

From Dev

Laravel: one to many relationship

From Dev

one to many relationship in laravel

From Dev

Related name for recursive many to many relationship not working

From Dev

How many to many relationship in Entity Framework is working

From Dev

Doctrine 2 Many To Many follower relationship not working

Related Related

  1. 1

    Rails 5: Many-to-Many Relationship Not Working

  2. 2

    Laravel 5.2.29 Many to Many relationship not working

  3. 3

    Laravel 5 Eloquent count many to many relationship

  4. 4

    one to many relationship in Laravel5 is not working properly

  5. 5

    Laravel - Many to Many relationship

  6. 6

    Many to many relationship in Laravel

  7. 7

    Many to Many relationship with Laravel

  8. 8

    Laravel - One to Many relationship is not working one way

  9. 9

    Laravel One to Many relationship not working - returns recursion

  10. 10

    Laravel - One to Many relationship is not working one way

  11. 11

    Laravel one to many model relationship not working

  12. 12

    Laravel 5 Eloquent Relationship - Has Many Though

  13. 13

    Laravel 5 Deleting a one-to-many relationship

  14. 14

    Laravel 5 one to many eloquent relationship

  15. 15

    Laravel 5 Eloquent Relationship - Has Many Though

  16. 16

    Building a dynamic query of a many-to-many relationship in Laravel 5

  17. 17

    Laravel 5 - access specific model on many to many relationship

  18. 18

    Empty data returning from many to many relationship laravel 5

  19. 19

    Laravel 5 - Dedicated Query string filtering on many-to-many Relationship

  20. 20

    Laravel 6 many to many relationship

  21. 21

    Pagination with many to many relationship in laravel

  22. 22

    Laravel many to many relationship error

  23. 23

    Laravel many to many relationship with conditions

  24. 24

    Laravel ORM many to many relationship

  25. 25

    Laravel: one to many relationship

  26. 26

    one to many relationship in laravel

  27. 27

    Related name for recursive many to many relationship not working

  28. 28

    How many to many relationship in Entity Framework is working

  29. 29

    Doctrine 2 Many To Many follower relationship not working

HotTag

Archive