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
Securing Laravel logo

Securing Laravel

The essential security resource for Laravel devs, covering everything you need to keep your apps secure. Sign up to receive weekly security tips and monthly in depth articles, diving deep into security concepts you need to know!

Securing Laravel
Blastup logo

Blastup

Blastup provides social media enhancement services including buying Instagram likes, followers, and views, with features like instant delivery and a variety of packages to suit different needs.

Blastup
The Certification of Competence for Laravel logo

The Certification of Competence for Laravel

A community-driven, proctored assessment across 4 levels designed to validate real-world Laravel knowledge, from Junior to mastery-level Artisan. Official Vue.js, Official Nuxt, Angular, React, JS certifications also available.

The Certification of Competence for Laravel
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
No Compromises logo

No Compromises

Joel and Aaron, the two seasoned devs from the No Compromises podcast, are now available to hire for your Laravel project. ⬧ Flat rate of $9500/mo. ⬧ No lengthy sales process. ⬧ No contracts. ⬧ 100% money back guarantee.

No Compromises
Celebian logo

Celebian

Celebian is a social media marketing agency specializing in helping their clients go viral on TikTok. Whether you're looking to reach a bigger audience or gain more Tiktok followers, likes, and views, they've got you covered.

Celebian