Polyscope - The agent-first dev environment for Laravel

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
Honeybadger logo

Honeybadger

Simple developer-focused application monitoring for Laravel. Error tracking, log management, uptime monitoring, status pages, and more!

Honeybadger
Curotec logo

Curotec

World class Laravel experts with GenAI dev skills. LATAM-based, embedded engineers that ship fast, communicate clearly, and elevate your product. No bloat, no BS.

Curotec
Tighten logo

Tighten

We help companies turn great ideas into amazing apps, products, and services.

Tighten
Laravel Cloud logo

Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Cloud
Get expert guidance in a few days with a Laravel code review logo

Get expert guidance in a few days with a Laravel code review

Expert code review! Get clear, practical feedback from two Laravel devs with 10+ years of experience helping teams build better apps.

Get expert guidance in a few days with a Laravel code review
CodeKudu logo

CodeKudu

Stand-ups, Retrospectives, and 360° Feedback for the entire team. 50% off with code LARAVELNEWS.

CodeKudu