PHPackages                             lexide/clay - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. lexide/clay

ActiveLibrary[Database &amp; ORM](/categories/database)

lexide/clay
===========

Create lightweight models and apply complex data structures to them

v3.1.2(7mo ago)211.4k↑30%2[1 PRs](https://github.com/lexide/clay/pulls)2MITPHPPHP &gt;=8.0CI passing

Since Jan 26Pushed 3mo ago1 watchersCompare

[ Source](https://github.com/lexide/clay)[ Packagist](https://packagist.org/packages/lexide/clay)[ Docs](https://github.com/lexide/clay)[ RSS](/packages/lexide-clay/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (2)Dependencies (4)Versions (31)Used By (2)

Clay
====

[](#clay)

Clay allows you to populate a model object with an array of data (from a database or other data store) without having to manually apply the values by hand. Data is applied to the model based on the name of each property, either directly or via setters if they are available. Clay supports nested array data and properties which contain other objects (in a multi layered data structure) as well as automatically converting keys to camel case when applying data to the model.

installation
------------

[](#installation)

Install via composer

```
composer require lexide/clay

```

Simple Example
--------------

[](#simple-example)

To use Clay, a model should use the ModelTrait trait and implement a method which calls loadData()

```
class Model
{
    use Lexide\Clay\Model\ModelTrait;

    public $property;
    ...

    public function __construct(array $data = [])
    {
        $this->loadData($data);
    }

}

```

The model can then be instantiated, passing data to the constructor

```
$data = ["property" => "value", ...];
$model = new Model($data);

echo $model->property; // outputs "value"

```

Data with underscored or spaced keys will be applied to their camel case counterparts:

```
$data = [
    "long_field_name" => ... // will apply to the property "longFieldName"
    "property name with spaces" => ... // will apply to "propertyNameWithspaces"
];

```

Object instantiation
--------------------

[](#object-instantiation)

If you want a subset of the data to be contained within a second class, you simply need to type-hint the supplied argument in a setter

```
class Address
{
    use Lexide\Clay\Model\ModelTrait;

    public $street;
    public $city;

    public function __construct(array $data = [])
    {
        $this->loadData($data);
    }
}

class Customer
{
    use Lexide\Clay\Model\ModelTrait;

    protected $address;

    public function __construct(array $data = [])
    {
        $this->loadData($data);
    }

    public function setAddress(Address $address)
    {
        $this->address = $address;
    }

}

$data = [
    "address": [
        "street" => "1 test street",
        "city" => "exampleton"
    ]
]

$customer = new Customer($data);
echo $customer->getAddress()->city; // outputs "exampleton"

```

Object collections
------------------

[](#object-collections)

The same can be done for an array or collection of objects. Clay looks for an "add" method for that property to determine the class to instantiate

```
class Customer
{
    use Lexide\Clay\Model\ModelTrait;

    protected $addresses = [];

    public function __construct(array $data = [])
    {
        $this->loadData($data);
    }

    public function setAddresses(array $addresses)
    {
        $this->addresses = [];
        foreach ($addresses as $address) {
            $this->addAddresses($address));
        }
    }

    public function addAddresses(Address $address)
    {
        $this->addresses[] = $address;
    }

}

$data = [
    "addresses": [
        [
            "street" => "1 Test street",
            "city" => "Exampleton"
        ],
        [
            "street" => "22 Sample row",
            "city" => "Testville"
        ]
    ]
]

$customer = new Customer($data);

```

###  Health Score

53

—

FairBetter than 97% of packages

Maintenance72

Regular maintenance activity

Popularity29

Limited adoption so far

Community16

Small or concentrated contributor base

Maturity79

Established project with proven stability

 Bus Factor1

Top contributor holds 57.9% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~150 days

Recently: every ~532 days

Total

27

Last Release

222d ago

Major Versions

0.1.0 → 1.0.02015-03-24

1.3.0 → 2.0.02018-02-16

2.2.0 → 3.0.02021-09-09

PHP version history (3 changes)0.1.0PHP &gt;=5.3.0

1.0.0PHP &gt;=5.4.0

3.0.0PHP &gt;=8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/a01f73f5eea8a755e31d879b50f5862ee8a20e8d73daba1c15cb7e75e68796db?d=identicon)[downsider](/maintainers/downsider)

---

Top Contributors

[![downsider](https://avatars.githubusercontent.com/u/4508388?v=4)](https://github.com/downsider "downsider (62 commits)")[![David-Denny](https://avatars.githubusercontent.com/u/35412695?v=4)](https://github.com/David-Denny "David-Denny (26 commits)")[![Danny-Smart](https://avatars.githubusercontent.com/u/113538899?v=4)](https://github.com/Danny-Smart "Danny-Smart (19 commits)")

---

Tags

modelmodelslightweight

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/lexide-clay/health.svg)

```
[![Health](https://phpackages.com/badges/lexide-clay/health.svg)](https://phpackages.com/packages/lexide-clay)
```

###  Alternatives

[mongodb/laravel-mongodb

A MongoDB based Eloquent model and Query builder for Laravel

7.1k7.2M71](/packages/mongodb-laravel-mongodb)[prettus/l5-repository

Laravel 5|6|7|8|9|10|11|12 - Repositories to the database layer

4.2k10.8M145](/packages/prettus-l5-repository)[spatie/laravel-translatable

A trait to make an Eloquent model hold translations

2.4k23.0M413](/packages/spatie-laravel-translatable)[spatie/eloquent-sortable

Sortable behaviour for eloquent models

1.5k22.9M268](/packages/spatie-eloquent-sortable)[venturecraft/revisionable

Keep a revision history for your models without thinking, created as a package for use with Laravel

2.6k6.6M51](/packages/venturecraft-revisionable)[pursehouse/modeler-laravel-eloquent

Generate model classes for Eloquent in Laravel

112.4k](/packages/pursehouse-modeler-laravel-eloquent)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
