1
After seeing a lot on the internet, I understood that I should not call a function of one controller in another and that the solution but elegant is to create a service, but I did not understand how I create the service and much less how I call its functions.
What I need to do is that when creating a simulated, I create a proposal and link to this simulated, but I already have a place that I create this proposal, in her contralator:
public function create(Request $proposta)
{
$this->valida($proposta);
$uuid = Str::uuid();
//load file
$file = $proposta->file('file');
// s3 storage
Storage::disk('s3')->putFileAs(
'proposta/',
$file,
$uuid . $file->getClientOriginalExtension(),
[
'visibility' => 'public',
'mimetype' => 'application/pdf'
]
);
//get sotarege url
$url = ('https://meuprovedor/proposta/' . $uuid . $file->getClientOriginalExtension());
$prop = Proposta::create([
'nome' => $proposta['nome'],
'numero' => $proposta['numero'],
'url' => $url,
'escola_id' => $proposta['escola_id'],
'grade_id' => $proposta['grade_id'],
]);
// Log::error(print_r($proposta['videos'],true));
// Log::error(print_r(json_decode($proposta['videos']),true));
foreach (json_decode($proposta['videos']) as $video) {
Video::create([
'ordem' => $video->ordem,
'titulo' => $video->titulo,
'video_url' => $video->video_url,
'proposta_id' => $prop->id
]);
}
return response()->json("Prosposta Criada", 200);
}
Now in my role that creates my simulation I need to create a proposal passing my request and get the id of the created proposal to move to creating the simulation (there in the commented part):
public function create(Request $simulado)
{
$this->valida($simulado);
$pieces = explode('T', $simulado->data);
return new SimuladoResource(Simulado::create([
'data' => Carbon::createFromFormat("Y-m-d", $pieces[0]),
'escola_id' => $simulado['escola_id'],
'proposta_id' => //função que cria uma proposta e retorna o id,
]));
}
my question is: Is there a php Artisan command to create this service? I just create it and then create a function inside and importing the file into my controllers I can already call the function? I ask this because in the references of Laravel, talks to create an interface, Register and a lot of other things, I do not understand what I have to do to have a unique function that tando the create of the proposal uses, when the create of the simulated use.
Service they say is any class that performs some logic. There are those who abuse the use of Service by laziness to correctly name that logic. In a way, you are already using a "service" within your controller with the $this->valida() method, the difference being that it would be in a separate class, roughly speaking. Moving this method to an Laravel Form Request, for example, would be as close as I can think of as an analogy to a service.
– gmsantos
Also, try to better organize the steps to create your entity. What they have in common is the logic to upload to S3? Extract and name this to reuse elsewhere if it makes sense.
– gmsantos
If you can also post the references you consulted, it also helps to understand how you imagined to solve this problem.
– gmsantos