Skip to content

[12.x] Add a new model cast named asFluent#56046

Merged
taylorotwell merged 1 commit intolaravel:12.xfrom
azim-kordpour:12.x
Jun 16, 2025
Merged

[12.x] Add a new model cast named asFluent#56046
taylorotwell merged 1 commit intolaravel:12.xfrom
azim-kordpour:12.x

Conversation

@azim-kordpour
Copy link
Contributor

@azim-kordpour azim-kordpour commented Jun 15, 2025

Hi,
Since the Fluent class was added to Laravel, I have been using the Fluent cast on some of my applications where I need to deal with JSON values in different ways. I think it would be great to have it in the framework.

Example:

namespace App\Models;

use Illuminate\Database\Eloquent\Casts\AsFluent;
use Illuminate\Database\Eloquent\Model;

class Passenger extends Model
{
    protected function casts(): array
    {
        return [
            'address' => AsFluent::class,
        ];
    }
}

It can be used:

 $passanger = Passenger::first();
 
 $passenger->address->get('office.street');
 $passenger->address->get('home.street');

It can be updated by passing the fluent object or array

use Illuminate\Support\Fluent;

 $passanger = Passenger::first();
 
 $passenger->address = new Fluent([
           'office' => ['street' => 'new street'],
           'office' => ['home' => 'new street']
])
 
 // by array
 $passenger->address = [
           'office' => ['street' => 'new street'],
           'office' => ['home' => 'new street']
]

I tried to follow the naming convention. Please let me know if there's anything wrong or if I can improve it.

@taylorotwell taylorotwell merged commit 8737a65 into laravel:12.x Jun 16, 2025
62 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants