Laravel Packages

Define Model Attributes With Laravel Fluent

Published
Define Model Attributes With Laravel Fluent image

Laravel Fluent is a package by Boris Lepikhin that provides an expressive way to define model attributes. This package automatically builds casts at runtime and gives you native auto-completion of model properties:

// Before
 
/**
* @property Collection $features
* @property float $price
* @property int $available
*/
class Product extends Model
{
protected $casts = [
'features' => 'collection',
'price' => 'float',
'available' => 'integer',
];
}
 
// After
use Based\Fluent\Fluent;
 
class Product extends Model
{
use Fluent;
 
public Collection $features;
public float $price;
public int $available;
}

This package supports all native PHP types and laravel primitive casts:

class Order extends Model
{
use Fluent;
 
public int $amount;
public Carbon $expires_at;
 
#[AsDecimal(2)]
public float $total;
 
#[Cast('encrypted:array')]
public array $payload;
}

This package can also handle model relations. You can learn more about this package, get full installation instructions, and view the source code on GitHub.

Paul Redmond photo

Staff writer at Laravel News. Full stack web developer and author.

Sponsored

laravelcloud logo
Laravel Cloud

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

Visit Laravel Cloud

The latest

View all →
CPX: The Composer Package Executor for PHP image

CPX: The Composer Package Executor for PHP

Read article
Laravel AI SDK Adds Human-in-the-Loop Tool Approval image

Laravel AI SDK Adds Human-in-the-Loop Tool Approval

Read article
Pest 5 Released With Test Impact Analysis, Agent Verification, and Evals image

Pest 5 Released With Test Impact Analysis, Agent Verification, and Evals

Read article
Queue-SQL: Run Mass Deletes and Updates Across Parallel Queue Jobs image

Queue-SQL: Run Mass Deletes and Updates Across Parallel Queue Jobs

Read article
Blade Formatting in Laravel Pint image

Blade Formatting in Laravel Pint

Read article
Inertia DevTools Is Now on the Chrome Web Store image

Inertia DevTools Is Now on the Chrome Web Store

Read article