Skip to content

Instantly share code, notes, and snippets.

@sombochea
Last active April 25, 2019 06:52
Show Gist options
  • Save sombochea/77c47f7acc83b325a5eedcf4bb895e2e to your computer and use it in GitHub Desktop.
Save sombochea/77c47f7acc83b325a5eedcf4bb895e2e to your computer and use it in GitHub Desktop.
Sample Setting Model in Laravel Eloquent with Except function
<?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