how do I select morphToMany relationship based on the morphable type?

Posted 11 months ago by nikocraft

I want to do something like this:

Tag::where('taggable_type', 'App\Models\Core\Media\Image')->get()

Image model has Tags, which are defined like

public function tags()
    return $this->morphToMany(Tag::class, 'taggable');

And then we have two migrations

    Schema::create('tags', function (Blueprint $table) {


    Schema::create('taggables', function (Blueprint $table) {

so ofc I cant do this Tag::where('taggable_type', 'App\Models\Core\Media\Image')->get()

since tag model it self does not have taggable_type, but the polymorph pivot table has. How can I query tag table but let it only return stuff where taggable_type is of this 'App\Models\Core\Media\Image'?

Please sign in or create an account to participate in this conversation.