lorisleiva / laravel-actions

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

`validateAttributes` throws scope exception #147

Closed nickfls closed 2 years ago

nickfls commented 2 years ago

Steps to repeat:

laravel new action-test
cd action-test
composer require lorisleiva/laravel-actions
php artisan make:action TestAction

and then in Action class:

namespace App\Actions\TestAction;

use Lorisleiva\Actions\Concerns\AsAction;
use Lorisleiva\Actions\Concerns\ValidateActions;
use Lorisleiva\Actions\Concerns\WithAttributes;

class TestAction
{
    use AsAction;
    use ValidateActions;
    use WithAttributes;

    public function rules(): array
    {
        return [
            'id' => 'required',
        ];
    }

    public function handle(array $attributes = []): array
    {
        $this->fill($attributes);

        return $this->validateAttributes();
    }
}

when executed as App\Actions\TestAction::run(['id'=>1]) will throw Call to protected method App\Actions\TestAction::prepareForValidation() from scope Illuminate\Container\BoundMethod in /home/vagrant/action-test/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php on line 36

nickfls commented 2 years ago

Thats because i am stupid and cannot read. Sorry, we are all good:

For a hy onlooker:

use Lorisleiva\Actions\Concerns\ValidateActions; will create this conflict

namespace App\Actions\TestAction;

use Lorisleiva\Actions\Concerns\AsAction;
use Lorisleiva\Actions\Concerns\WithAttributes;

class TestAction
{
    use AsAction;
    use WithAttributes;

    public function rules(): array
    {
        return [
            'id' => 'required',
        ];
    }

    public function handle(array $attributes = []): array
    {
        $this->fill($attributes);

        return $this->validateAttributes();
    }
}

will work just fine.

Sorry again!