Created
February 5, 2020 05:51
-
-
Save tzkmx/3c78971fe557509e65b1e23d0ef124bf to your computer and use it in GitHub Desktop.
filter Eloquent Collection higher order with argument
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$collectionFiltered = $collection->filter->updatedAt($date); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment