I have a question regarding Laravel and my project is the function that receives 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 one certificate in the same event, another is not generated if it does not, then it is generated.
The problem is that anyway (if you already have usuario
in the event or if there is not) the system drops in else
( error generating ).