Creating model record in multiple locations

Published 1 month ago by JuanRangel

I am using a ConsultationController to create a Consultation record. I now want to be able to create a consultation record in my RegisterController. What is the best way to handle this?

A few options that come to mind:

  1. Add a method on the User model to handle this. $user->startConsultation()
  2. Use the Consultation::create() in all locations.

What is the best practice here? Which class should have the responsibility of creating a consultation?

Best Answer (As Selected By JuanRangel)
PricelessRabbit

the service is a general class created by you an then injected in the controller. For example:

  • Simple case:

Create a service class that do what you need (I put all my Services in App\Services )


class ConsultationService {

   public function createForUser(User $user){//TODO
   }

}

Inject it in the controllers ( laravel ioc bind it automatically )

class MyController {

private $consultationService;

__construct(ConsultationService $consultationService){

    $this->consultationService = $consultationService;

}

public create(){

    $user = // retrieve the user
    
    $consultation = $this->consultationService->createForUser($user)
}

}

  • Complex case:

You can create an interface of the service and then bind it in AppServiceProvider with a concrete implementation. So if you want to change the implementation of the service you only need to change the bind in the service provider.

For example i have a FileServiceContract interface with a method saveFile() and some implementations of the interface that process the file in different ways.

$this->app->when(ProfileImageController::class)->needs(FileServiceContract::class)->give(ProfileImageFileService::class);
PricelessRabbit

You can also extract all the consultations management in a ConsultationService, and then inject it via ioc in the controllers that needs it.

In that way, if you need more complexity than "create" to create a new consultation, you can encapsulate all the code in a single place

JuanRangel

@PricelessRabbit Ah, that's actually a great idea. I hadn't thought of that. I have not used a service like this before. Can you share an example?

PricelessRabbit

the service is a general class created by you an then injected in the controller. For example:

  • Simple case:

Create a service class that do what you need (I put all my Services in App\Services )


class ConsultationService {

   public function createForUser(User $user){//TODO
   }

}

Inject it in the controllers ( laravel ioc bind it automatically )

class MyController {

private $consultationService;

__construct(ConsultationService $consultationService){

    $this->consultationService = $consultationService;

}

public create(){

    $user = // retrieve the user
    
    $consultation = $this->consultationService->createForUser($user)
}

}

  • Complex case:

You can create an interface of the service and then bind it in AppServiceProvider with a concrete implementation. So if you want to change the implementation of the service you only need to change the bind in the service provider.

For example i have a FileServiceContract interface with a method saveFile() and some implementations of the interface that process the file in different ways.

$this->app->when(ProfileImageController::class)->needs(FileServiceContract::class)->give(ProfileImageFileService::class);
JuanRangel

Thanks, @PricelessRabbit I've implemented this in my codebase. I appreciate the help.

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