Laravel 5.3 - Model that can be created by multiple Controllers

3

Good afternoon, I use Laravel 5.3 as a back-end for a project and I will try to explain the problem more succinctly.

Problem : In my project I have the Duplicate model and the Contract.

  • A contract creates multiple duplicates by ContractController
  • A duplicate can be created by another controller called LocacaoController.
  • And finally the duplicate can be created by the DuplicataController via store () method.
  • In the database I have the tables:

    duplicatas
    contratos
    

    They all share basically the same code

    $duplicata = new Duplicata()
    $duplicata->valor = $request->valor
    $duplicata->descricao = $request->descricao
    $duplicata->contrato_id = $request->contrato_id
    $duplicata->save()
    

    But now I'm going to add our Number to the database, and I'll have to copy and paste the programming logic into all 3 controllers.

    And I find this a bad programming practice and I intend to unify the way in which duplicates are created, but how do I do this? Am I doing everything wrong?

        
    asked by anonymous 27.01.2017 / 19:24

    4 answers

    5

    Create this method within Controller of Duplicata :

    public function criaDuplicata($request)
    {
        $duplicata = new Duplicata()
        $duplicata->create($request);
        return $duplicata;
    }
    
    public function store(Request $request)
        {
            $this->criaDuplicata($request->all());
            return view("suaView", []);
        }
    

    And call it even on your store that gets a Request . So you can use this dependency in other places, for example in ContratoController .

    public function __construct(DuplicataController $duplicataController) //Aqui está a injeção de dependência
    {
        $this->duplicataController = $duplicataController;
    }
    
    public function geraDuplicata($duplicata)
    {
        $this->duplicataController->criaDuplicata($duplicata);
    }
    
        
    27.01.2017 / 19:36
    2

    IMHO has been shown to be the best), static method, so let's go one more way and you'll see which one fits you best.

    What you need can be done from inheritance:

    abstract class podeDuplicar extends BaseController{
    
        public function gerarDuplicata(){
            $duplicata = new Duplicata();
            $duplicata->valor = $request->valor;
            $duplicata->descricao = $request->descricao;
            $duplicata->contrato_id = $request->contrato_id;
            $duplicata->save();
        }
    
    }
    

    So, just give extend :

    class ContratoController extends podeDuplicar { /* Resto da lógica aqui */ }
    class LocacaoController extends podeDuplicar { /* Resto da lógica aqui */ }
    class DuplicataController extends podeDuplicar { /* Resto da lógica aqui */ }
    

    So anyone can just do it:

    (new ContratoController)->gerarDuplicata();
    (new LocacaoController)->gerarDuplicata();
    (new DuplicataController)->gerarDuplicata();
    

    And if you want to keep store() of DuplicataController , just pull the method:

    class DuplicataController extends podeDuplicar {
        public function store(){ 
            $this->gerarDuplicata();
        }
    }
    
        
    27.01.2017 / 19:53
    2

    I've created a folder called Classes within App . And I created a file with the name of the class.

    Type in your case can be CadastroDuplicata .

    <?php
    
        namespace App\Classes;
    
        class CadastroDuplicata{
            public static function create($request){
                $duplicata              = new Duplicata()
                $duplicata->valor       = $request->valor
                $duplicata->descricao   = $request->descricao
                $duplicata->contrato_id = $request->contrato_id
                $duplicata->save();
            }
        }
    

    Controller

    use App\Classes\CadastroDuplicata
    
    .
    .
    .
    
    CadastroDuplicata::create($request);
    
    Hence you call the function on any other Controller so you can create more parameters as well. Do not forget to call the Models class in Class .

    Better yet you can do this in the Model of Duplicata . Create this same function in there instead of creating a class in a separate folder.

    I did it this way because I like it.

        
    27.01.2017 / 19:38
    2

    I would then traits , which is a practice used in framework , where% co- can inherit this code.

    Example

    controllers

    trait DuplicatasOfControllers
    {
        public function inserirDuplicata(Request $request) 
        {
            $duplicata              = new Duplicata()
            $duplicata->valor       = $request->valor
            $duplicata->descricao   = $request->descricao
            $duplicata->contrato_id = $request->contrato_id
            $duplicata->save();
        }
    }
    

    trait

    class ContratoController 
    {
        use DuplicatasOfControllers;
    }
    
    class DuplicataController 
    {
        use DuplicatasOfControllers;
    
        public function store(Request $request)
        {
            $this->inserirDuplicata($request);
            return view("suaView", []);
        }
    }
    
    class LocacaoController
    {
        use DuplicatasOfControllers;
    }
    

    Changes made to the duplicate code will be made to controllers .

        
    27.01.2017 / 22:16