lorisleiva / laravel-actions

⚡️ Laravel components that take care of one specific task
https://laravelactions.com
MIT License
2.41k stars 117 forks source link

feat: add static runWithAttributes method #250

Closed jaulz closed 4 months ago

jaulz commented 11 months ago

This helper method allows users to run an action with attributes and validation.

lorisleiva commented 11 months ago

Hi there, I'm not too sure about this approach to be honest because it assumes the Action's attributes and the handle's arguments are the same which they may not be.

jaulz commented 11 months ago

Ah, sorry, I missed to pass the required arguments. Now, only the arguments that are found on the method's signature will be passed to the handle method.

nickfls commented 7 months ago

@jaulz as far as I can tell, you can achieve something like this with:

ActionClass::make()
    ->fill(['foo' => 'bar'])
    ->set('variable', value)
    ->handle(['other' => 'attributes'])

I sometimes use it when I want syntax sugar and better readability, like AddUserProfile around StoreUserProfile:

/**
     * @param \App\Models\Contracts\Profile|\App\Models\Profile $profile
     * @param array $attributes
     * @return \App\Models\Contracts\UserProfile
     */
    public function handle(Profile $profile, $attributes = []): UserProfile
    {
        return StoreUserProfile::make()
            ->fill($attributes)
            ->handle(['profile_id' => $profile->getKey()]);
    }

but i recognize that your use case may be different