Created
November 21, 2018 09:38
-
-
Save rogercbe/f138c09fcb01c7c0626a909250afcf55 to your computer and use it in GitHub Desktop.
Laravel throw exception when setting a property that doesnt exist
This file contains hidden or 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; | |
| use Exception; | |
| use Illuminate\Database\Eloquent\Model as Eloquent; | |
| class Model extends Eloquent | |
| { | |
| protected $default = ['id']; | |
| protected $defined = ['firstname', 'lastname']; | |
| public function setAttribute($key, $value) | |
| { | |
| $defined = array_merge($this->$defined, $this->default); | |
| if (! in_array($key, $defined)) { | |
| throw new Exception("Attribute '{$key}' has not been defined."); | |
| } | |
| return parent::setAttribute($key, $value); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment