Add BaseModel with stricter attribute existence checking
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class BaseModel extends Model
|
||||
{
|
||||
public function getAttribute($key)
|
||||
{
|
||||
if (! $key) {
|
||||
throw new \LogicException("No attribute ".var_export($key, true));
|
||||
}
|
||||
|
||||
return parent::getAttribute($key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a relationship.
|
||||
*
|
||||
* @param string $key
|
||||
* @return mixed
|
||||
*/
|
||||
public function getRelationValue($key)
|
||||
{
|
||||
if (!method_exists($this, $key)) {
|
||||
throw new \LogicException("No attribute or relation ".var_export($key, true));
|
||||
}
|
||||
|
||||
return parent::getRelationValue($key);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user