belongsToMany(MetroStation::class, $in->getTable(), 'branch_id', 'station_id'); } protected static function booted() { parent::booted(); static::creating(function ($self) { /** @var $self static */ if (empty($self->num) && ! empty($self->city_id)) { $self->num = (int)$self->where('city_id', $self->city_id)->max('num') + 1; } }); } }