如何在 Laravel eloquent 中查询枢轴 table 上的计数

How to query count on the pivot table in Laravel eloquent

我在数据库中有两个数据table。一个是用户 table,另一个是 属性 table。用户可以根据需要保存任意数量的属性。此外,一个 属性 可以被多个用户保存。所以有一个枢轴 table,其中有 property_id 和 user_id 作为记录。现在想查询热门楼盘。超过100个用户保存的所有属性都是受欢迎的属性。我该怎么做?像我在做的事情

public function popularProperties(Request $request)
    {
       $id = $request->id;
        return Property::where('saved'->count(),'>',100)->with([
            'paymentPlan' => function ($query){
                $query->select('property_id','all_in_one_go','down_payment','minimum_installment_per_month');
            },
            'city',
            'propertyType' => function ($query){
                $query->select('id','name');
            },
            'user' => function ($query){
            $query->select('id','name','email');
        },
        'images' => function($query){
            $query->select('imageable_id','url');
        }
        ])->withCount('likes')->get();
    }

以下代码选择所有 属性 的计数 > 100。 whereIn 内部是一个子查询,它选择所有 属性 个存在 > 100 次的 ID。请注意,您必须填写正确的 Table 和列名

$propertys = Property::whereIn('id', function ($q){
            $q->select('property_id')
            ->from('user_property')
            ->groupBy('property_id')
            ->havingRaw('COUNT(*) > 100');
})->get();