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

NiloLeon's avatar

ELoquent and count in a pivot table

Hi i have two tables with this relationships

Refunds

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Refunds extends Model
{
	public $primaryKey = 'id';
	protected $fillable = [
		'date_ref',
		'status_ref',
    ];

	public function services()
        {
            return $this->belongsToMany(Services::class)->withPivot(['services_id','services_amount','services_status']);
        }		
        
    public function claims()
        {
            return $this->belongsTo(Claims::class,'claims_id');
        }           

}

and Services

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Services extends Model
{
	public $primaryKey = 'id';
	protected $fillable = [
        'code',
        'name',
    ];
	
	public function refunds()
    {
		return $this->belongsToMany(Refunds::class);
    }

}

i have this pivot table

+------------+-------------+-----------------+-----------------+--+
| refunds_id | services_id | services_amount | services_status |  |
+------------+-------------+-----------------+-----------------+--+
|          1 |          61 |              56 | ok              |  |
+------------+-------------+-----------------+-----------------+--+
|          1 |          76 |              43 | ko              |  |
+------------+-------------+-----------------+-----------------+--+
|          1 |          87 |              43 | ok              |  |
+------------+-------------+-----------------+-----------------+--+
|          2 |          63 |             876 | ok              |  |
+------------+-------------+-----------------+-----------------+--+
|          4 |         541 |              30 | ok              |  |
+------------+-------------+-----------------+-----------------+--+
|          3 |         541 |              45 | ko              |  |
+------------+-------------+-----------------+-----------------+--+
|          3 |          15 |              67 | ok              |  |
+------------+-------------+-----------------+-----------------+--+
|          3 |        1974 |             344 | ok              |  |
+------------+-------------+-----------------+-----------------+--+
|          5 |        1764 |             130 | ko              |  |
+------------+-------------+-----------------+-----------------+--+
|          5 |        1973 |              70 | ok              |  |
+------------+-------------+-----------------+-----------------+--+
|          6 |           5 |               7 | ko              |  |
+------------+-------------+-----------------+-----------------+--+
|          7 |        2763 |            NULL | NULL            |  |
+------------+-------------+-----------------+-----------------+--+
|          8 |        2764 |            NULL | NULL            |  |
+------------+-------------+-----------------+-----------------+--+
|          9 |         609 |              23 | ok              |  |
+------------+-------------+-----------------+-----------------+--+
|         10 |          12 |              34 | ko              |  |
+------------+-------------+-----------------+-----------------+--+
|         10 |         608 |              34 | ok              |  |
+------------+-------------+-----------------+-----------------+--+
|         17 |        2763 |               1 | ok              |  |
+------------+-------------+-----------------+-----------------+--+
|         16 |           3 |              34 | ok              |  |
+------------+-------------+-----------------+-----------------+--+
|         13 |        2763 |            NULL | NULL            |  |
+------------+-------------+-----------------+-----------------+--+
|         14 |        2751 |              38 | ok              |  |
+------------+-------------+-----------------+-----------------+--+
|         18 |        2764 |            NULL | ok              |  |
+------------+-------------+-----------------+-----------------+--+
|         19 |        2763 |            NULL | ok              |  |
+------------+-------------+-----------------+-----------------+--+
|         20 |        2764 |            NULL | NULL            |  |
+------------+-------------+-----------------+-----------------+--+
|         21 |         574 |              23 | NULL            |  |
+------------+-------------+-----------------+-----------------+--+
|         21 |           6 |              23 | ok              |  |
+------------+-------------+-----------------+-----------------+--+

how can i count when SERVICES_ID is 2763 ? Thx

0 likes
2 replies
bugsysha's avatar
bugsysha
Best Answer
Level 61

Easiest way is to run php artisan make:model RefundService -p. That will create pivot model that you can use as normal model. Add relationships to it since it belongs to Services and Refunds models. And you can do

RefundService::query()->where('service_id', 2763)->get();

Please or to participate in this conversation.