I have an application which uses APIs as its data sources.
I'm considering trying out Laravel, but I can't really find any reference that discusses how models that don't use a database should be handled.
So, any suggestions?
Give a try to Jens Segers's laravel-model.
It provides an eloquent-like base class
that can be used to build custom models in Laravel 4.
Jenssegers\Model
like Illuminate\Database\Eloquent\Model
implements ArrayAccess, ArrayableInterface, JsonableInterface.
class User extends Model {
protected $hidden = array('password');
public function save()
{
return API::post('/items', $this->attributes);
}
public function setBirthdayAttribute($value)
{
$this->attributes['birthday'] = strtotime($value);
}
public function getBirthdayAttribute($value)
{
return date('Y-m-d', $value);
}
public function getAgeAttribute($value)
{
$date = DateTime::createFromFormat('U', $this->attributes['birthday']);
return $date->diff(new DateTime('now'))->y;
}
}
$item = new User(array('name' => 'john'));
$item->password = 'bar';
echo $item; // {"name":"john"}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With