8 months ago

Service Class & Container Best Practices

Posted 8 months ago by lambooni

I have a structure as follows:

Model: XeroInvoice

I use this to:

  • Interact with the database i.e. $xeroInvoice->syncInvoice()
  • Perform actions on the model. i.e. $xeroInvoice->lockAndSend() changes a row in the DB and then starts a queue job which in turn communicates to Xero via an external API to lock and send the invoice.

Service Class: XeroService

I use this to:

  • A set of functions that link the Xero API to my models. I.e. $xeroService->updateCreateContact(User $user) where user is my user model. In all cases I interact with Xero via my service class and the Xero API interacts with my models via the service class also.

Controller: XeroInvoiceController

Not really used. I have one method that converts PDF data into a view.

My question is:

I ideally want an API such as $xeroInvoice->getPdfData. This method must go through my service class to interact with the Xero API and retrieve the data. Laravel does not allow me to inject my XeroService class into the model and I feel this is probably for a reason. I can use app()->make(XeroService::class); to get it, but this feels hacky.

What is the best way to go about setting this up?

Please sign in or create an account to participate in this conversation.