jenssegers / model by jenssegers

An Laravel eloquent-like model class, for Laravel and other frameworks
3,200,585
380
9
Package Data
Maintainer Username: jenssegers
Package Create Date: 2013-03-30
Package Last Update: 2024-07-30
Home Page:
Language: PHP
License: MIT
Last Refreshed: 2024-11-22 03:03:15
Package Statistics
Total Downloads: 3,200,585
Monthly Downloads: 54,656
Daily Downloads: 2,043
Total Stars: 380
Total Watchers: 9
Total Forks: 54
Total Open Issues: 19

Model

Build Status Coverage Status

This model provides an Laravel eloquent-like base class that can be used to build custom models in Laravel or other frameworks.

Features

  • Accessors and mutators
  • Model to Array and JSON conversion
  • Hidden attributes in Array/JSON conversion
  • Guarded and fillable attributes
  • Appending accessors and mutators to Array/JSON conversion
  • Attribute casting

You can read more about these features and the original Eloquent model on http://laravel.com/docs/eloquent

Installation

Install using composer:

composer require jenssegers/model

Example

class User extends Model {

    protected $hidden = ['password'];

    protected $guarded = ['password'];

    protected $casts = ['age' => 'integer'];

    public function save()
    {
        return API::post('/items', $this->attributes);
    }

    public function setBirthdayAttribute($value)
    {
        $this->attributes['birthday'] = strtotime($value);
    }

    public function getBirthdayAttribute($value)
    {
        return new DateTime("@$value");
    }

    public function getAgeAttribute($value)
    {
        return $this->birthday->diff(new DateTime('now'))->y;
    }
}

$item = new User(array('name' => 'john'));
$item->password = 'bar';

echo $item; // {"name":"john"}