Last active
April 25, 2019 06:52
-
-
Save sombochea/77c47f7acc83b325a5eedcf4bb895e2e to your computer and use it in GitHub Desktop.
Sample Setting Model in Laravel Eloquent with Except function
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; | |
class Setting extends BaseModel | |
{ | |
protected $fillable = ['key', 'value']; | |
// Need to remove this variables before save to db. | |
protected $excepts = ['type']; | |
public $timestamps = false; | |
/** | |
* Overrided Function Save for Custom Implement | |
* For Save Type.Key | |
*/ | |
public function save(array $options = []) | |
{ | |
$split_key = \explode('.', $this->key); | |
$check_key_has_type = count($split_key) == 2; | |
$this->key = $check_key_has_type ? | |
$this->key : !is_null($this->type) ? | |
$this->type . '.' . $this->key : 'default' . '.' . $this->key; | |
// If you don't want to use $excepts variable, uncomment this line below | |
// $this->except(['type']); | |
return parent::save($options); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment