8000 [11.x] Add `Macroable` and `fill()` to `Support\Fluent` by stevebauman · Pull Request #54404 · laravel/framework · GitHub
[go: up one dir, main page]

Skip to content

[11.x] Add Macroable and fill() to Support\Fluent #54404

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 5 commits into from
Jan 29, 2025

Conversation

stevebauman
Copy link
Contributor
@stevebauman stevebauman commented Jan 29, 2025

Description

This PR adds the ability to add macros to the Fluent class, as well as a fill method for easily filling a Fluent instance with an array of attributes, as you're used to with Eloquent Models.

Macro Usage

A really cool usage of Fluent that I've found would be very useful to have is a macro'ed validate method:

Fluent::macro('validate', function (array $rules, ...$params) {
    return validator($this->all(), $rules, ...$params)->validate();
});

You could use this in Action classes, instead of having to pull in the validator yourself.

For example, consider this existing Laravel Fortify action:

namespace App\Actions\Fortify;

// ...

class CreateNewUser implements CreatesNewUsers
{
    // ...

    public function create(array $input): User
    {
        Validator::make($input, [
            // ...
        ])->validate();

        return DB::transaction(...);
    }
}

This could be refactored to:

(new CreateNewUser)->create($request->fluent());
// ...

class CreateNewUser implements CreatesNewUsers
{
    // ...
    
    public function create(Fluent $input): User
    {
        $fluent->validate([
            // ...
        ]);

        return DB::transaction(...);
    }
}

Fill Usage

The fill acts as you'd expect it to, as it just performs the same logic that was previously in its constructor:

$fluent = new Fluent(['name' => 'John Doe']);

$fluent->fill([
    'email' => 'johndoe@email.com',
    'age' => 30,
]);

//[
//    'name' => 'John Doe'
//    'email' => 'johndoe@email.com',
//    'age' => 30,
//]
$fluent->all();

@taylorotwell taylorotwell merged commit 1cd9bf4 into laravel:11.x Jan 29, 2025
38 checks passed
@calebdw
Copy link
Contributor
calebdw commented Jan 31, 2025

This is causing failures:

  In Fluent.php line 19:                                                       
                                                                               
                                                                               
    Declaration of Laravel\Nova\Support\Fluent::fill(array $attributes) must   
  be                                                                           
     compatible with Illuminate\Support\Fluent::fill($attributes)   

@stevebauman
Copy link
Contributor Author

@calebdw Ok, it looks like Nova may just need to remove the override 👍

@ProRisk
Copy link
ProRisk commented Jan 31, 2025

y la version de laravel 5 tendra solucionado esto ?

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.

4 participants
0