I have a Model with a protected $withCount
property, and it always gets into an infinite loop. If remove that property, the model will return data. The project I am working in has another Model using $withCount
and it returns the child object count fine. I’ve combed through the working model and compared it with mine, and as far as I can tell I have set everything up correctly. Any idea (or hints to chase down) why this would always get an infinite loop?
<?php
namespace AppModels;
use DateTime;
use IlluminateDatabaseEloquentModel;
use IlluminateDatabaseEloquentRelationsHasMany;
use IlluminateDatabaseEloquentRelationsBelongsTo;
use IlluminateDatabaseEloquentSoftDeletes;
/**
* @property Team team
* @property string name
* @property string notes
* @property HasMany<ShiftAssignment> shiftAssignments
* @property DateTime created_at
* @property DateTime modified_at
* @property DateTime deleted_at
*
* @method static where(string $string, mixed $id)
*/
class Calendar extends Model
{
use SoftDeletes;
/**
* @return BelongsTo<Team> Get the team for the Calendar
*/
public function team(): BelongsTo {
return $this->belongsTo(Team::class);
}
/**
* @return HasMany Get all the relationships for the calendar.
*/
public function shiftAssignments(): HasMany {
return $this->hasMany(ShiftAssignment::class);
}
/**
* If we want any relationship counts to be return automatically, add them here.
*/
protected $withCount = [
'shift_assignments'
];
/**
* Get the attributes that should be cast.
*
* @return array<string, string>
*/
protected function casts(): array
{
return [
'created_at' => 'datetime:Y-m-dTh:i:sZ',
'modified_at' => 'datetime:Y-m-dTh:i:sZ',
'deleted_at' => 'datetime:Y-m-dTh:i:sZ',
];
}
}
These both produce an infinite loops:
Calendar::with('team_id', $team->id);
Calendar::all();