Gani Siva kumar
Gani Siva kumar

Reputation: 161

How to get allTags() in laravel from a filtered model

I have two models Song and Album.

Song is Taggable(using eloquent-taggable).

1)Song belongs_to Album.

Album has columns name and language.

I want to get all tags of Song for a particular Album language.

I tried

Song::join('albums','albums.id','=','songs.album_id')
     ->where('albums.language', 'hindi')
     ->allTags()->orderBy('count', 'DESC')->get(); 

But not working.

Upvotes: 0

Views: 372

Answers (2)

Sapna Bhayal
Sapna Bhayal

Reputation: 802

At first create relation on song and album as :

namespace App;

use Illuminate\Database\Eloquent\Model;
use Cviebrock\EloquentTaggable\Taggable;

    class Song extends Model
    {
        use Taggable;
        protected $fillable = ['name','title','album_id'];
        public $timestamps = false;

        public function album() {
            return $this->belongsTo(Album::class, 'album_id');
        }
    }

and then make query as :

$songs = Song::with(['album','tags'])->whereHas('album', function($q) {
                    $q->where('language', '=', 'hindi');
                })->get();

This will returns you all songs which belongs to albums which has "hindi" language with their tags.

Upvotes: 1

Rwd
Rwd

Reputation: 35190

allTags is a static method that should be called on the model directly. When you start to add constraints or chain on relationship method to an Eloquent model an instance of Builder will be returned.

I would suggest, if you haven't already done so, to create a Tag model that extends \Cviebrock\EloquentTaggable\Models\Tag and then define a relationship for the songs. Your model will then look something like:

namespace App;

class Tag extends \Cviebrock\EloquentTaggable\Models\Tag
{
    /**
     * Songs Relationship.
     *
     * @return \Illuminate\Database\Eloquent\Relations\MorphToMany
     */
    public function songs()
    {
        return $this->morphedByMany(Song::class, 'taggable', 'taggable_taggables', 'tag_id', 'taggable_id');
    }
}

If you already have the model but don't have the relationship then simply add the above relationship to that model.

Then you should be able to get all of the Tags with:

 $tags = Tag::whereHas('songs.artist', function ($query) {
    $query->where('language', 'hindi');
})->orderBy('count', 'DESC')->get();

If you don't want to create a new model that you could do something like:

Song::with('tags')
    ->whereHas('artist', function ($query) {
        $query->where('language', 'hindi');
    })
    ->get()
    ->flatMap(function ($song) {
        return $song->tags;
    })
    ->unique()
    ->sortByDesc('count');

For more information on collection please have a look at the documentation

Upvotes: 1

Related Questions