Skip to content

Instantly share code, notes, and snippets.

@tzkmx
Created February 5, 2020 05:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tzkmx/3c78971fe557509e65b1e23d0ef124bf to your computer and use it in GitHub Desktop.
Save tzkmx/3c78971fe557509e65b1e23d0ef124bf to your computer and use it in GitHub Desktop.
filter Eloquent Collection higher order with argument
<?php
namespace App\Model;
use Carbon\Carbon;
/**
* Trait UpdatedAfterTrait
* @package App\Model
* @method string getCreatedAtColumn
* @method string getUpdatedAtColumn
* @method mixed getAttribute(string $key)
*/
trait UpdatedAfterTrait
{
public function updatedAfter(Carbon $date = null): bool
{
if (is_null($date)) return true;
$createdAtKey = $this->getCreatedAtColumn();
$updatedAtKey = $this->getUpdatedAtColumn();
$createdAt = $this->getAttribute($createdAtKey);
$updatedAt = $this->getAttribute($updatedAtKey);
return $date->lessThanOrEqualTo($createdAt)
|| $date->lessThanOrEqualTo($updatedAt);
}
}
<?php
$collectionFiltered = $collection->filter->updatedAt($date);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment