rtconner / laravel-tagging

Tag support for Laravel Eloquent models - Taggable Trait
MIT License
882 stars 168 forks source link

Retrieve all of the models associated with a tag #123

Closed devendragohil closed 6 years ago

devendragohil commented 8 years ago

I want to retrieve all of the models that at associated with provided tag. Is it possible?

For example, for Article model I have created a tag foo and also for Author model I have created the same tag foo. Now is there any method that providing tag name foo will return all the models associated with it i.e. Article and Author.

jgardezi commented 7 years ago

@devendragohil did you figured out about this. I am also looking for a similar solution.

jgardezi commented 7 years ago

@devendragohil I got the solution for it ... read through this documentation its self explanatory https://laravel.com/docs/5.1/eloquent-relationships#polymorphic-relations. I have also post the example below as well.

$data = []; $results = Tagged::where('tag_slug', $slug)->get(); if($results) { foreach ($results as $key => $result) { $data[$key] = $result->taggable; } return $data; }

In above case Tagged Model is linked to the dynamic pivot table, tagging_tagged. The method taggable() in the Tagged Model retrieve the owner of a polymorphic relation from the polymorphic model.

Hope the above will be helpful for you.

Kind regards, Javed Gardezi