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

FounderStartup's avatar

Eloquent query issue ....

I am developing a real estate site. It has the concept of INNER CIRCLES which has a user as an ADMIN and users can join different CIRCLES. I need to show total number of users ( including ADMIN ) in a CIRCLE , the total number of LISTINGS by all the CIRCLE members and total number of LISTINGS by all the members which are CLOSED.

Controller :

 public function MemberInnerCircles()
    {

        $user = Auth::user();
        $memberof = InnerCircleMember::where('member_id', $user->id)->where('status', 1)->paginate(10);

        return view('frontend.innercircle.memberinnercircles', compact('memberof', 'user'));
    } // end method

model:


class InnerCircleMember extends Model
{
    use HasFactory;
    protected $guarded = [];

     public function member(){
        return $this->belongsTo(User::class,'member_id');
    }


    public function circle(){
        return $this->belongsTo(InnerCircle::class,'circle_id');
    }


}

What will be my query ?

0 likes
12 replies
MohamedTammam's avatar
$circles = InnerCircleMember::withCount('members')->get();

You should get the number as $circles[0]->members_count

If you want to add to your current query

$memberof = InnerCircleMember::withCount('members')->where('member_id', $user->id)->where('status', 1)->paginate(10);
1 like
FounderStartup's avatar

@MohamedTammam This is what exactly I need :

I am developing a real estate site. It has the concept of INNER CIRCLES which has a user as an ADMIN and users can join different CIRCLES.

  1. I need to show total number of users ( including ADMIN ) in a CIRCLE
  2. The total number of LISTINGS by all the CIRCLE members
  3. Total number of LISTINGS by all the members which are CLOSED.
FounderStartup's avatar

@MohamedTammam

NO

model : User

public function circlemember() { return $this->hasMany(InnerCircleMember::class, 'member_id', 'id'); }

model listings has a relation :

public function user(){ return $this->belongsTo(User::class,'user_id','id'); }

FounderStartup's avatar

@MohamedTammam Getting this error :

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'users.inner_circle_member_id' in 'where clause'

Please or to participate in this conversation.