James Star
James Star

Reputation: 85

Laravel 5 morphToMany morphedByMany returning empty array

Okay, I think I've set up everything I'm supposed to including composer dump auto-load and still getting empty array. Any ideas? Does morphToMany still work in Laravel 5?

I have two models > Post and Tag and have namespace them correctly.. e.g. namespace App; and when referencing with morphToMany I have used 'App\Tag' or 'App\Post' where it is required. Anything else I could be possibly missing, it looks like it should be pretty straight forward to accomplish.

I can return both Models data individually but calling relationship methods, I get nothing, empty array even though their associated correctly in the db.

Thanks.

Upvotes: 1

Views: 960

Answers (1)

Md Imthiaj Habib
Md Imthiaj Habib

Reputation: 31

I was having the exact same issue with morphToMany relationship as the relationship query was returning an empty array. Then I found this issue ticket, https://github.com/laravel/framework/issues/4006which helped me to resolve my problem.

So I had to save the full NameSpaced entity in the taggables table

tag_id -> <id of tag>
taggable_id -> <id of post>
taggable_type -> 'App\Post'

This worked for me.

But in your case, its seems more like a many to many relationship rather than Polymorphic many to many relationship.

Hope this helps. Thanks

Upvotes: 3

Related Questions