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