@ultrawelfare People these days tend to use “DTOs” for passing parameters around layers of their application. I put “DTO” in quotation marks because it’s not the true purpose of what a DTO was actually conceived for, but the gist is you treat it as a parameter bag object that can only be instantiated with valid data. Therefore, if you have an instance of it, you know its contents have already been pre-validated.
So, your services and actions would take an instance of some sort of DTO/parameter bag object, and you can use its contents however you need:
class CustomerService
{
public function create(CreateCustomerData $data)
{
// Use contents of $data to create customer object
}
}
You can then create instances of this CreateCustomerData object in a controller, console command, or queued job and then pass it to your service class method:
public function store(StoreCustomerRequest $request)
{
// Create customer data from request data...
$data = CreateCustomerData::fromArray($request->validated());
$customer = $this->customers->create($data);
// Return response...
}
public function handle(): int
{
// Create customer data from console command arguments...
$data = CreateCustomerData::fromArray($this->arguments());
$this->customers->create($data);
$this->info('Customer created.');
return Command::SUCCESS;
}