Say I implement https://github.com/sebastiaanluca/laravel-boolean-dates/blob/master/src/BooleanDates.php and a custom trait that does something similar for public dates (i.e. attribute input/output transformation; nothing related to resources btw).
Created
August 18, 2018 12:24
-
-
Save sebastiaanluca/67036fc7809190072e0a72765cc9e2e8 to your computer and use it in GitHub Desktop.
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 | |
declare(strict_types=1); | |
namespace App\Models; | |
use SebastiaanLuca\BooleanDates\BooleanDates; | |
abstract class User extends Model | |
{ | |
// Have to rename each getAttribute/setAttribute method | |
use BooleanDates, HasPublicDates { | |
BooleanDates::getAttribute as getBooleanDateAttribute; | |
HasPublicDates::getAttribute as getPublicDateAttribute; | |
} | |
/** | |
* Get an attribute from the model. | |
* | |
* @param string $key | |
* | |
* @return mixed | |
*/ | |
public function getAttribute($key) | |
{ | |
if (! $key) { | |
return null; | |
} | |
// Have to call each method in a specific order manually | |
// for each class that uses attribute methods | |
if ($this->hasBooleanDate($key)) { | |
return $this->getBooleanDate($key); | |
} | |
if ($this->hasPublicDate($key)) { | |
return $this->getPublicDate($key); | |
} | |
return parent::getAttribute($key); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment