Test of variable true or false

0

I have a controller that I may or may not receive a request If I receive it I will use it

It turns out that it is always entering the condition, even when it is false I'm testing a true or false value What's wrong?

    $turmas_todas = isset($request->todas) ? $request->todas : true;

    if ($turmas_todas) {
        print_r($turmas_todas);
        $value[$i]['id_turma']     = '';
        $value[$i]['codigo_turma'] = 'TODAS';
    }

Is there another better way to do it in Laravel?

    
asked by anonymous 19.07.2018 / 16:22

3 answers

1

The problem might be $request->todas . Try running the following command and see the value it prints.

print_r($request->todas);

If you see an error, it's probably because you're trying to access a private attribute directly.

What I think is happening is that the "all" attribute is private and you are trying to access it directly. If this is happening regardless of the value of the attribute, isset($request->todas) will return true . So you would need a method that would return the value of the attribute.

    
19.07.2018 / 22:49
1

Laravel has a very expressive form that should be used to better read the code.

        //verifica se todas existe no request
        if(request()->has('todas')){

            //verificar se o valor de todas é verdadeiro
            if (request()->get('todas')){
                print_r(request()->get('todas'));
                $value[$i]['id_turma'] = '';
                $value[$i]['codigo_turma'] = 'TODAS';
            }
        }
    
19.07.2018 / 23:00
0

The correct one would be:

$turmas_todas = isset($request->todas) ? (($request->todas == 'false') ? false : $request->todas) : true;

In this way, if the value of $request->todas comes with a string "false", $turmas_todas will receive false.

    
19.07.2018 / 16:28