Support the ongoing development of Laravel.io →
Database Eloquent Views

Hello, I have Users who can belong to many Groups, and Issues which are assigned to a single Group.

When displaying a list of issues, I'd like to be able to say, if this issue is assigned to any of the groups a user belongs to, display or not. I'd also like to be able to display a count of all of the issues associated with a user (via groups). Remember, a user can belong to multiple groups. I'm just not entirely sure of the syntax for something like this. I would ideally like to be able to say something like

if($issue->assigned_to == anyOf($user->groups))

and to count them something like count($issues->assignedToMe)

but aren't quire sure how to go about that, or if there's anything built in to eloquent that lets me do this in an easier way. Currently I'm thinking of just foreach looping through the groups, checking against it, but seems really wasteful. Here are my models:

User.php

class User extends Model implements AuthenticatableContract, CanResetPasswordContract {

	use Authenticatable, CanResetPassword;

	/**
	 * The database table used by the model.
	 *
	 * @var string
	 */
	protected $table = 'users';

	/**
	 * The attributes that are mass assignable.
	 *
	 * @var array
	 */
	protected $fillable = ['name', 'email', 'password'];

	/**
	 * The attributes excluded from the model's JSON form.
	 *
	 * @var array
	 */
	protected $hidden = ['password', 'remember_token'];

	
    public function groups()
	{
		return $this->belongsToMany('App\Group');
	}

	public function belongsToGroup($group)
	{
		return in_array($group, array_fetch($this->groups->toArray(), 'name'));
	}
}

Group.php

class Group extends Model {

	/**
	 * The database table used by the model.
	 *
	 * @var string
	 */
	protected $table = 'groups';

	/**
	 * The attributes that are mass assignable.
	 *
	 * @var array
	 */
	protected $fillable = ['name'];

	/**
	 * The attributes excluded from the model's JSON form.
	 *
	 * @var array
	 */
	protected $hidden = [];

	public function users()
	{
		return $this->belongsToMany('App\User');
	}

	public function issues()
	{
		return $this->hasMany('App\Issue');
	}
}

Issue.php

class Issue extends Model {

	/**
	 * The database table used by the model.
	 *
	 * @var string
	 */
	protected $table = 'issues';

	/**
	 * The attributes that are mass assignable.
	 *
	 * @var array
	 */
	protected $fillable = ['author_id', 'assigned_to_id', 'project_id', 'version', 'reference', 'type', 'description', 'status', 'priority'];

	/**
	 * The attributes excluded from the model's JSON form.
	 *
	 * @var array
	 */
	protected $hidden = [];

	public function project()
	{
		return $this->belongsTo('App\Project', 'project_id');
	}

	public function author()
	{
		return $this->belongsTo('App\User', 'author_id');
	}

	public function assigned_to()
	{
		return $this->belongsTo('App\Group', 'assigned_to_id');
	}

	public function assigned_to_me()
	{
		return $this->belongsTo('App\Group', 'assigned_to_id')
			->where('assigned_to_id', '=', Auth::user()->groups());
	}
	public function assigned()
	{
		return $this->assigned_to->name;
	}

}

Any help would be much appreciated.

Last updated 3 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.