Model

jenssegers/model image

Model stats

Downloads
2.5M
Stars
348
Open Issues
21
Forks
54

View on GitHub →

An Laravel eloquent-like model class, for Laravel and other frameworks

Model

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

 
use Jenssegers\Model\Model;
 
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"}
jenssegers photo

Engineering Manager & Solution Architect at OTA Insight

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.


Jenssegers Model Related Articles

Laravel 5 Eloquent attribute casting image

Laravel 5 Eloquent attribute casting

Read article
LoadForge logo

LoadForge

Scalable load testing for web apps & APIs. Simulate real-world traffic and identify breaking points and performance limits with powerful, scalable load tests designed for Laravel.

LoadForge
Typesense Search logo

Typesense Search

Typesense is an open source, blazing-fast search engine, optimized for helping you build delightful search experiences for your sites and apps. Natively integrated with Laravel Scout.

Typesense Search
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit
Statamic logo

Statamic

The drop-in ready Laravel CMS you’re been waiting for. Go full-stack or headless, flat file or database – it’s up to you.

Statamic
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum