Skip to content

Instantly share code, notes, and snippets.

@rogercbe
Created November 21, 2018 09:38
Show Gist options
  • Select an option

  • Save rogercbe/f138c09fcb01c7c0626a909250afcf55 to your computer and use it in GitHub Desktop.

Select an option

Save rogercbe/f138c09fcb01c7c0626a909250afcf55 to your computer and use it in GitHub Desktop.
Laravel throw exception when setting a property that doesnt exist
<?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