Laravel - Function on Controller

0

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 ).

    
asked by anonymous 16.10.2017 / 12:39

3 answers

0
public function existeCertificado($id, $id_evento)
{
    $existeCert = DB::table('certificados')
             ->where([['id_usuario', $id], ['id_evento', $id_evento]])
             ->first();

    if(is_null($existeCert))
    {
        return TRUE;
    } 
    else
    {
        return FALSE;
    }        
}

If you replace get () with first (), laravel will return the value found, or a null, if you do not have .. just a glance at the doc, I think it might be more interesting to use Eloquent,

It would also be nice to do the comparison using the is_null () php function, which serves exactly to tell if a value is null or not:

is_null () php: link
< Laravel: link

    
28.06.2018 / 03:38
-1

Your code is always returning TRUE because the get() method of Eloquent returns a collection, not a Boolean value. That is, even if there are no records that satisfy your query, the return will always be TRUE . You should check if this collection is empty or not so that your logic makes sense.

    
16.10.2017 / 13:30
-1

I believe that just inserting the firstOrNew function solves the problem.

        $certificado = certificado::firstOrNew(['id_evento' => $request->id_evento]);
        $certificado->id_evento = $request->id_evento;
        $certificado->qualidade = $request->qualidade;
        $certificado->id_usuario = $id;
        $certificado->save();
        $pusher = App::make('pusher');
    
22.12.2017 / 14:05