iLexN
2/26/2018 - 9:40 AM

Collection helper for model

<?php

class Videos extents \Illuminate\Database\Eloquent\Collection
{
  public function getTotalLengthInSeconds() : int
  {
    return $this->sum('length');
  }
  
  public function getTotalLengthForHumans() : string
  {
    diff = Carbon::now->diff(
      Carbon::now()->addSeconds($this->getTotalLengthInSeconfs())
    );
    $example_string = 'for humans';
    return $example_string;
  }
}
<?php

class Video extends \Illuminate\Database\Eloquent\Model
{
  public function new Collection(array $models = [])
  {
    return new Videos($models);
  }
}
<?php

$s = Video::where('series',1)->get();
$st = $s->getTotalLengthForHumans();