Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 16, 2025

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