PHPackages                             proai/laravel-struct - 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. proai/laravel-struct

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

proai/laravel-struct
====================

A Laravel package for using structs with Laravel including attribute casting.

v0.1.7(11mo ago)21.9k↓50%MITPHPPHP ^8.0

Since Jan 10Pushed 11mo ago1 watchersCompare

[ Source](https://github.com/ProAI/laravel-struct)[ Packagist](https://packagist.org/packages/proai/laravel-struct)[ Docs](http://github.com/proai/laravel-struct)[ RSS](/packages/proai-laravel-struct/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (8)Dependencies (2)Versions (9)Used By (0)

Laravel Struct
==============

[](#laravel-struct)

[![Latest Stable Version](https://camo.githubusercontent.com/bfb63cbe506ba496f8ffea61fff6a5395430920f26b5eb6272d6cab347bd13cd/68747470733a2f2f706f7365722e707567782e6f72672f70726f61692f6c61726176656c2d7374727563742f762f737461626c65)](https://packagist.org/packages/proai/laravel-struct) [![Total Downloads](https://camo.githubusercontent.com/32d48975e4b32b07d797e2a51009a2344b17afd88f20755f888f78f4a35bfc12/68747470733a2f2f706f7365722e707567782e6f72672f70726f61692f6c61726176656c2d7374727563742f646f776e6c6f616473)](https://packagist.org/packages/proai/laravel-struct) [![Latest Unstable Version](https://camo.githubusercontent.com/8280cc4d520d4b8bf0e049c15d757cb6d286e8c30fb94cf17a6c7d4379e98623/68747470733a2f2f706f7365722e707567782e6f72672f70726f61692f6c61726176656c2d7374727563742f762f756e737461626c65)](https://packagist.org/packages/proai/laravel-struct) [![License](https://camo.githubusercontent.com/32021c4f642364468aefc9c77ca309094fa19a6f8881052e3d2359d685a835bf/68747470733a2f2f706f7365722e707567782e6f72672f70726f61692f6c61726176656c2d7374727563742f6c6963656e7365)](https://packagist.org/packages/proai/laravel-struct)

A struct is a collection of typed variables. Structs are a well known datatype in other programming languages, but unfortunately not natively part of PHP yet. This package aims to bring structs to PHP and in particular to Laravel.

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require proai/laravel-struct
```

Please note that you need at least **PHP 7.4** and **Laravel 8** for this package.

Usage
-----

[](#usage)

The package uses named properties, which were introduced in PHP 7.4, to define a struct:

```
use App\Structs\GeoLocation;
use ProAI\Struct\Struct;

class Address extends Struct
{
    public string $street;

    public string $city;

    public GeoLocation $geo_location;
}
```

You can use all primitive types like `string`, `bool`, `float`, `int`, but also you can type a property as an object. The object can also be another struct, which enables you to nest structs (like `GeoLocation` above).

Structs are instantiated by using an array of values:

```
$address = new Address([
    'street' => 'Baker Street',
    'city' => 'London',
    'geo_location' => [
        'latitude' => 51.52,
        'longitude' => -0.1566,
    ],
]);
```

Properties that are typed as objects will be converted to these objects on instantiation. Thus in the example above an object of `App\Structs\GeoLocation` will be created for the `$geo_location` property.

Properties can be accessed normally:

```
$address->street
=> "Baker Street"

$address->country
=> App\Structs\GeoLocation { ... }
```

*Hint: Snake cased properties are used to mimic the behaviour of Eloquent attributes.*

### Attribute Casting

[](#attribute-casting)

You can use attribute casting with structs in your Eloquent models:

```
use App\Structs\Address;
use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    protected $casts = [
        'address' => Address::class,
    ];
}
```

In order to make this work you need to define a `json` column of the specified key, so in this case `address`.

Alternatively you can use the composed struct caster by adding the argument `composed` to compose a struct from multiple columns:

```
use App\Structs\Address;
use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    protected $casts = [
        'address' => Address::class.':composed',
    ];
}
```

The column names must start with the specified key followed by an underscore and the property name. This also works with nested structs. For the example above we would need the following columns:

```
address_street
address_city
address_geo_location_latitude
address_geo_location_longitude

```

Finally you can also write your own custom caster by overwriting the `castUsing` method of the struct like described in the Laravel docs.

### Collections

[](#collections)

Sometimes you need an array of structs. For this purpose you can define a struct collection. The struct collection class is inherited from the Laravel collection class, so you can use all methods of a Laravel collection.

```
use App\Structs\Address;
use ProAI\Struct\Collection;

class AddressCollection extends Collection
{
    public $type = Address::class;
}
```

By the way, attribute casting also works with struct collections for `json` columns.

Support
-------

[](#support)

Bugs and feature requests are tracked on [GitHub](https://github.com/proai/laravel-struct/issues).

License
-------

[](#license)

This package is released under the [MIT License](LICENSE).

###  Health Score

36

—

LowBetter than 82% of packages

Maintenance50

Moderate activity, may be stable

Popularity22

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity53

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% 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 ~228 days

Total

8

Last Release

354d ago

PHP version history (3 changes)v0.1.0PHP &gt;=7.4

v0.1.3PHP &gt;=7.4|^8.0

v0.1.5PHP ^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/54da07fafc9e45d312ea3094f27da887d232ec5de9b28b5a3073eb76b7874214?d=identicon)[markusjwetzel](/maintainers/markusjwetzel)

---

Top Contributors

[![markusjwetzel](https://avatars.githubusercontent.com/u/6650637?v=4)](https://github.com/markusjwetzel "markusjwetzel (10 commits)")

---

Tags

laravelValue Objecteloquentattribute castingstruct

### Embed Badge

![Health badge](/badges/proai-laravel-struct/health.svg)

```
[![Health](https://phpackages.com/badges/proai-laravel-struct/health.svg)](https://phpackages.com/packages/proai-laravel-struct)
```

###  Alternatives

[cviebrock/eloquent-sluggable

Easy creation of slugs for your Eloquent models in Laravel

4.0k13.6M253](/packages/cviebrock-eloquent-sluggable)[tucker-eric/eloquentfilter

An Eloquent way to filter Eloquent Models

1.8k4.8M26](/packages/tucker-eric-eloquentfilter)[watson/validating

Eloquent model validating trait.

9723.3M47](/packages/watson-validating)[cybercog/laravel-ban

Laravel Ban simplify blocking and banning Eloquent models.

1.1k651.8k11](/packages/cybercog-laravel-ban)[cybercog/laravel-love

Make Laravel Eloquent models reactable with any type of emotions in a minutes!

1.2k302.7k1](/packages/cybercog-laravel-love)[cviebrock/eloquent-taggable

Easy ability to tag your Eloquent models in Laravel.

567694.8k3](/packages/cviebrock-eloquent-taggable)

PHPackages © 2026

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