jeudi 4 avril 2019

How do i switch a custom implementation of a interface on a condition in http controller in Laravel

I have method on a controller where basically just storing a form to the database. My form is very big and it has more than 30-40 fields. So i need to store this information in different 3 tables on a condition.

For example :

foreach($request->all() as $answer):

    if($answer->employeeType === 1){

    //store data to type_one_table

   } else if($answer->employeeType === 2){

     //store data to type_two_table

   } else if($answer->employeeType === 3){

     //store data to type_two_table

  } else {

    //store data to some_other_table

    }

endforeach;

So i was thinking to use some custom StoreInterface which will have some store method.

and extract above logic to its own implementation.

And then in my Controllers constructor receive that interface instance.

But how i can call which implementation do i need for particular condition.

Can i do this a controller ?

Or Should i use different strategy in this case.

please guide me.

Thanks.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire