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

NiloLeon's avatar

Laravel count under relationship

Hello I have 3 tables and 1 pivot table. Models are

Claims

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use App\Models\Bigcust;
use App\Models\RefundsServices;

class Claims extends Model
{
    public $primaryKey = 'id';
    protected $fillable = [
        'dossier',
        'date_cla',
        'closed_at',
        'bigcust',
        'mcs',
        'sap',
        's6',
        'user'
    ];

    //Add extra attribute
    protected $attributes = ['bigcust_name'];

    //Make it available in the json response
    protected $appends = ['bigcust_name'];

    public function refunds()
    {
        return $this->hasMany(Refunds::class);
    }

    public function customers()
    {
        return $this->belongsTo(Customusers::class, 'customers_id');
    }

    //implement the attribute
    public function getBigcustNameAttribute()
    {
        $bigcust = Bigcust::where('id', '=', $this->bigcust)->first();
        if (is_null($bigcust)) {
            return ;
        } else {
            return $bigcust->name;
        }
    }

    public function refundsservices()
    {
        return $this->hasManyThrough(
            RefundsServices::class, 
            'App\Models\Refunds',
            'claims_id',
            'refunds_id',
            'id',
            'id',
        );
    }

}

Refunds

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

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

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

}

and Services

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

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

}

So I've a one to many relationship between Claims and Refunds then Many to many relationship between Refunds and Services Starting from Claims how can I count how many services has each Refunds of claims. I tried to define also pivot Model RefundsServices but I cannot use WITHCOUNT in blade correctly In blade I'd like to see for Each claims their refunds and for each refunds the counting of services related to refunds Thanks

0 likes
2 replies
NiloLeon's avatar
NiloLeon
OP
Best Answer
Level 1

I've resolved this way

 @foreach ($claim->refunds as $refund)
                @forelse ($refund->services as $item)
                @empty
                //If there is no services values for a refund do this
                <i class="fa-solid fa-lock-open fa-lg" style="color: #37517e"></i>
                @endforelse
@endforeach
1 like
Hassankhan's avatar
	I have solve this problem
	public function services()
    {
        return $this->belongsToMany(Services::class)
            ->withPivot(['refunds_id','services_id','note', 'services_amount','services_status','id'])->count();
    }		

Please or to participate in this conversation.