0
I have a question regarding Laravel and a project of mine being the function that receives the information from a form in which the administrator chooses the event and the usuarios
(multiple select
) to generate the certificate (stored in the table certificados
- columns id
/ id_usuario
/ id_evento
)
public function gerar(Request $request)
{
$id_evento = $request->id_evento;
$id_participantes = $request->input('participantes');
foreach ($id_participantes as $id)
{
if($this->existeCertificado($id, $id_evento))
{
$certificado = new Certificado();
$certificado->id_evento = $request->id_evento;
$certificado->qualidade = $request->qualidade;
$certificado->id_usuario = $id;
$certificado->save();
$pusher = App::make('pusher');
//default pusher notification.
//by default channel=test-channel,event=test-event
//Here is a pusher notification example when you create
//a new resource in storage.
//you can modify anything you want or use it wherever.
$pusher->trigger('test-channel',
'test-event',
['message' => 'Certificado $evento->titulo_evento !!']);
return redirect('certificado')->with('status', 'Certificados gerados!');
}
else
{
return redirect('certificado')
->with('status', 'Erro ao gerar alguns certificados!');
}
}
}
This is the function that searches in my database if the user (id_usuario
) already has some certificate in the event (id_evento
):
public function existeCertificado($id, $id_evento)
{
$existeCert = DB::table('certificados')
->where([['id_usuario', $id], ['id_evento', $id_evento]])
->get();
if($existeCert == NULL)
{
return TRUE;
}
else
{
return FALSE;
}
}
If the user already has a certificate in the same event, no other is generated if it has not, it is generated.
The problem is that anyway (if there is already record of the usuario
in the event or if there is no) the system falls in the else
(error while generating).
At first the code seems to be correct, already tried to give a dd() (var_dump of the Laravel) in the received variables in the function before making the query to check if they are with correct values or come empty or in array form?
– Darlei Fernando Zillmer
Every time two or more participants are chosen your code will bugar ... !!! because it makes a foreach and executes a redirect it does not perform all for ... You observed this detail?
– novic