Skip to content
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

Use container to call Data::authorize() to allow for dependencies #910

Merged
merged 2 commits into from
Jan 17, 2025

Conversation

cosmastech
Copy link
Contributor

@cosmastech cosmastech commented Dec 5, 2024

I want to build a request object that leverages Laravel's new contextual attributes. For instance:

class CreatePodcast extends Data
{
    public function __construct(
        public string $name,
        public bool $premium
    ) {
    }

    public static function authorize(
        #[CurrentUser] User $user,
        #[RouteParameter('org')] Organization $org
    ): bool
    {
        return $user->can('createPodcastFor', $org);
    }
}

While it's possible to add a custom DataPipe to pipelines(), this seems like it would be a pretty common use-case for folks.

@rubenvanassche
Copy link
Member

Thanks!

@rubenvanassche rubenvanassche merged commit c7807b7 into spatie:main Jan 17, 2025
1 check 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