Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

david001's avatar

how to query this

Hi ,I have two table users and tags. I want to get all user that has at one least tag.

tags table


user_id   tag
1             php
1             laravel
2            php

controller


$tags = $request->get('tags');
on dd($tags) I get
array:1 [▼
  0 => "laravel,php,js"
]
My query
			$users = User::where('status',1);

			  $users->when($tags, function ($query, $tags ) {
                return $query->where(function ($whereQuery) use ($tags ) {
                    foreach ($tags as $tag) {
                       Tag::where('tag',$tag)
                     
                    }

                });

            });

$users = $users->get();



I am not getting expected result

0 likes
9 replies
SilenceBringer's avatar

@david001

$users = User::where('status',1);
    ->when($tags, function ($query, $tags ) {
            return $query->whereHas(fn ($query) => $query->whereIn('name', explode(',', $tags[0]));
    })
    ->get();
david001's avatar

@silencebringer . this is not working for me. My php version is 7.3 I have tags relationship in User.php

User.php


public function tags(){
        return $this->hasMany(Tag::class);
    }

I have users in users table and tags in tags table

david001's avatar

got error: message: "Call to undefined method Closure::getRelationExistenceQuery()"

        $users->when($tags, function ($query, $tags ) {
            return $query->whereHas(function ($query) use($tags){
                return Tag::whereIn('name', explode(',', $tags[0]));
            });
        });
SilenceBringer's avatar
Level 55

@david001 for tags you show us

array:1 [▼
  0 => "laravel,php,js"
]

so, array with 1 comma-separated value. Let's take first value $tags[0] and parse it

dd(explode(',', $tags[0]));

and we will have array

array:3 [
  0 => "laravel",
  1 => "php",
  2 => "js"
]

now we can use it in whereIn condition

$query->whereIn('name', explode(',', $tags[0]))

and now we can apply it inside whereHas relationship existance check

$query->whereHas(function ($query) use($tags){
	return Tag::whereIn('name', explode(',', $tags[0]));
});

let's use it only if tags presented

	->when($tags, function ($query, $tags) {
    	$query->whereHas('tags', function ($query) use ($tags) {
        	$query->whereIn('tag', explode(',', $tags[0]));
    	});
    })

let's add this to User model query - and you will have exacty what @michaloravec wrote

$users = User::where('status', 1)->when($tags, function ($query, $tags) {
    $query->whereHas('tags', function ($query) use ($tags) {
        $query->whereIn('tag', explode(',', $tags[0]));
    });
})->get();

What else do you expect?

webrobert's avatar

In your modal relation …

User > hasMany > tag

Then do


$users = User::has('tag')->get();

// only users that have at least one tag are returned in the collection

Please or to participate in this conversation.