0

I am trying to setup the following relationsship:

Comment (user_id) ->belongTo User ->belongsToMany ->Groups

(Pivot: group_user)

I want to be able to fetch all groups associated with the comment with: Comment::find(i)->groups. Also Comment::wherehas('groups') and Comment::Where(groups in [1,3])

I've been looking at https://github.com/staudenmeir/eloquent-has-many-deep#belongsto But can't get it to work as intended.

 public function groups()
    {

         return $this->hasManyDeep('App\Group', ['group_user', 'App\User']);
    }

Laravel 7

Line in Linus
  • 390
  • 8
  • 25

2 Answers2

0

From github discussion:

class Comment extends Model
{
    use \Staudenmeir\EloquentHasManyDeep\HasRelationships;

    public function groups()
    {
        return $this->hasManyDeep(
            Group::class
            [User::class, 'group_user'],
            ['id'],
            ['user_id']
        );
    }
}

Works like a charm!

Line in Linus
  • 390
  • 8
  • 25
-1

This may help you

//'tables1','tables2','tables3' are relations in USER model & 'COL 7' is column of table 3

$x=USER::where('status','active')->with('tables1','tables2','tables3')->whereHas('tables3', function($q)
                {
                    $q->where('COL 7',"US");

                })->get();
                dd($x);
Rana Nadeem
  • 1,115
  • 1
  • 9
  • 17