/12-setting-getters.php Secret
Last active
April 28, 2022 14:27
12 - User settings getters
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 | |
/** | |
* @property array $settings | |
* @property array $social_links | |
*/ | |
class User extends Authenticatable | |
{ | |
... | |
protected $casts = [ | |
'settings' => 'array' | |
]; | |
public function setting(string $name, $default = null) | |
{ | |
if (array_key_exists($name, $this->settings ?? [])) { | |
return $this->settings[$name]; | |
} | |
return $default; | |
} | |
public function getSocialLinksAttribute(): array | |
{ | |
return array_filter([ | |
'personal' => $this->setting('social_personal'), | |
'twitter' => $this->setting('social_twitter'), | |
'facebook' => $this->setting('social_facebook'), | |
'instagram' => $this->setting('social_instagram'), | |
'linkedin' => $this->setting('social_linkedin'), | |
'reddit' => $this->setting('social_reddit'), | |
]); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment