1
I have a boletosController controller need to call the Function create in the tradeController;
boletosController.php;
namespace App\Http\Controllers;
use App\Repositorios\Boletos;
use Illuminate\Support\Facades\Request;
class BoletosController extends Controller
{
protected $boletos;
public function __construct(Boletos $boletos)
{
$this->boletos = $boletos;
}
public function create($data)
{
$create = $this->boletos->create($data);
return $create;
}
}
How best to call the create function in the trading controller:
tradeController.php
namespace App\Http\Controllers;
use App\Models\Boletos;
use App\Models\Negotiation;
class NegociacaoController extends Controller
{
public function index()
{
}
public function gerarBoletosNegociacao()
{
//chamar aqui uma função de outro controller
}
The best way is not to call. If your controller has a greater responsibility than just handling the http request, then there is something wrong. Logic should be implemented in another structure, usually called service.
– Woss
For example, why not call the method
create
from your repository instead of calling from the controller?– Woss
Then in that case I would have to reverse, call the Trading model and deal within the controller that generates the billets?
– André Cabral
As already mentioned here in the comments, apparently there is something wrong there, the correct thing is you create a
Service
and put this function in it, and then you "inject" as dependency the service in the 2 controllers.– Kayo Bruno