Convert and save time in bank with PHP

0

I have an input field in which the user types an hour (eg "08:00") to convert this string to strtotime () and convert it back to date () and saved in mysql in TIME format. So far so good, though I think you should have a better way to do it. The problem is that if the input field is empty it saves in the "00:00:00" bank and it should not save anything. If you do an if (if ($ sfi1)) to know if strtotime returns false and use date () only if something exists, everything works correctly. But I want to know if it has a better way than using if on each input.

public function store(Request $request)
{
    $input = $request->all();
    $this->modality = new Modality($input);

    $sfi1 = strtotime($request->input('segunda-feira-inicio1'));
    if($sfi1) {
        $this->modality->segunda_feira_inicio1 = date("H:i:s", $sfi1);
    }
    $sff1 = strtotime($request->input('segunda-feira-fim1'));
    $this->modality->segunda_feira_fim1 = date("H:i:s", $sff1);

    $this->modality->save();
    return redirect()->action('ModalityController@index');
}

Form:

<div class="container">
<div class="panel panel-primary">
    <div class="panel-heading">Modalidades</div>
    <div class="panel-body">
        <form method="post" action="{{ route('store') }}" enctype="multipart/form-data">
            <input type="hidden" name="_token" value="{{ csrf_token() }}">
            <div class="form-group">
                <label for="exampleInputEmail1">Nome</label>
                <input type="text" class="form-control" id="exampleInputEmail1" placeholder="Modalidade" name="name">
            </div>
            <div class="form-group">
                <label for="exampleInputEmail1">Descrição</label>
                <textarea class="form-control" rows="5" placeholder="Descrição" name="description"></textarea>
            </div>
            <div class="form-group">
                <label for="exampleInputFile">Upload de imagens</label>
                <input type="file" name="photo[]" multiple >
                <p class="help-block">Para selecionar multiplas imagens segure Crtl e escolha as imagens</p>
            </div>
            <div class="table-responsive">
                <label for="exampleInputFile">Horários</label>
                <table class="table">
                    <tr>
                        <th>Dia da Semana</th>
                        <th>Início 1</th>
                        <th>Fim 1</th>
                        <th>Início 2</th>
                        <th>Fim 2</th>
                        <th>Início 3</th>
                        <th>Fim 3</th>
                        <th>Início 4</th>
                        <th>Fim 4</th>
                    </tr>
                    <tr>
                        <td>Segunda-feira</td>         
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="segunda-feira-inicio1">
                            </div>
                        </td>
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="segunda-feira-fim1">
                            </div>
                        </td>
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="segunda-feira-inicio2">
                            </div>
                        </td>
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="segunda-feira-fim2">
                            </div>
                        </td>
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="segunda-feira-inicio3">
                            </div>
                        </td>
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="segunda-feira-fim3">
                            </div>
                        </td>
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="segunda-feira-inicio4">
                            </div>
                        </td>
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="segunda-feira-fim4">
                            </div>
                        </td>
                    </tr>
                    <tr>
                        <td>Terça-feira</td>         
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="terca-feira-inicio1">
                            </div>
                        </td>
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="terca-feira-fim1">
                            </div>
                        </td>
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="terca-feira-inicio2">
                            </div>
                        </td>
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="terca-feira-fim2">
                            </div>
                        </td>
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="terca-feira-inicio3">
                            </div>
                        </td>
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="terca-feira-fim3">
                            </div>
                        </td>
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="terca-feira-inicio4">
                            </div>
                        </td>
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="terca-feira-fim4">
                            </div>
                        </td>
                    </tr>
                    <tr>
                        <td>Quarta-feira</td>         
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="quarta-feira-inicio1">
                            </div>
                        </td>
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="quarta-feira-fim1">
                            </div>
                        </td>
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="quarta-feira-inicio2">
                            </div>
                        </td>
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="quarta-feira-fim2">
                            </div>
                        </td>
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="quarta-feira-inicio3">
                            </div>
                        </td>
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="quarta-feira-fim3">
                            </div>
                        </td>
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="quarta-feira-inicio4">
                            </div>
                        </td>
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="quarta-feira-fim4">
                            </div>
                        </td>
                    </tr>
                    <tr>
                        <td>Quinta-feira</td>         
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="quinta-feira-inicio1">
                            </div>
                        </td>
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="quinta-feira-fim1">
                            </div>
                        </td>
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="quinta-feira-inicio2">
                            </div>
                        </td>
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="quinta-feira-fim2">
                            </div>
                        </td>
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="quinta-feira-inicio3">
                            </div>
                        </td>
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="quinta-feira-fim3">
                            </div>
                        </td>
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="quinta-feira-inicio4">
                            </div>
                        </td>
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="quinta-feira-fim4">
                            </div>
                        </td>
                    </tr>
                    <tr>
                        <td>Sexta-feira</td>         
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="sexta-feira-inicio1">
                            </div>
                        </td>
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="sexta-feira-fim1">
                            </div>
                        </td>
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="sexta-feira-inicio2">
                            </div>
                        </td>
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="sexta-feira-fim2">
                            </div>
                        </td>
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="sexta-feira-inicio3">
                            </div>
                        </td>
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="sexta-feira-fim3">
                            </div>
                        </td>
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="sexta-feira-inicio4">
                            </div>
                        </td>
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="sexta-feira-fim4">
                            </div>
                        </td>
                    </tr>
                    <tr>
                        <td>Sábado</td>         
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="sabado-inicio1">
                            </div>
                        </td>
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="sabado-fim1">
                            </div>
                        </td>
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="sabado-inicio2">
                            </div>
                        </td>
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="sabado-fim2">
                            </div>
                        </td>
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="sabado-inicio3">
                            </div>
                        </td>
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="sabado-fim3">
                            </div>
                        </td>
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="sabado-inicio4">
                            </div>
                        </td>
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="sabado-fim4">
                            </div>
                        </td>
                    </tr>
                </table>
            </div>
            <button type="submit" class="btn btn-primary">Adicionar modalidade</button>
        </form>
    </div>
</div>

    
asked by anonymous 06.07.2017 / 19:43

2 answers

2

With Laravel it is possible to obtain the list of all the values passed in the request, with the exception of some, through the method:

$request->except([...]);

So, first we start by storing this list in a variable:

$fields = $request->except(["_token", "name", "description", "photo"]);

To go through all the values, simply use a loop of repetition:

foreach($fields as $field => $value) {
    // ...
}

If the field value is empty, update the column to NULL :

foreach($fields as $field => $value) {
    if(empty($value)) {
        $value = NULL;
    } else {
        // ...
    }
}

We have converted the time to the desired format:

foreach($fields as $field => $value) {
    if(empty($value)) {
        $value = NULL;
    } else {
        $column = str_replace('-', '_', $field);
        $value = date("H:i:s", strtotime($value));
    }
}

The names of the fields are in segunda-feira-inicio1 format, but the column names are in segunda_feira_inicio1 format, that is, to get their column name from the field name just replace - with _ :

foreach($fields as $field => $value) {
    if(empty($value)) {
        $value = NULL;
    } else {
        $column = str_replace('-', '_', $field);
        $value = date("H:i:s", strtotime($value));
    }

    $column = str_replace('-', '_', $field);
}

And then we assign the value to the column:

foreach($fields as $field => $value) {
    if(empty($value)) {
        $value = NULL;
    } else {
        $column = str_replace('-', '_', $field);
        $value = date("H:i:s", strtotime($value));
    }

    $column = str_replace('-', '_', $field);
    $this->modality->{$column} = $value;
}

So your complete method should look like this:

public function store(Request $request)
{
    $input = $request->all();
    $this->modality = new Modality($input);

    $fields = $request->except(["_token", "name", "description", "photo"]);

    foreach($fields as $field => $value) {
        if(empty($value)) {
            $value = NULL;
        } else {
            $column = str_replace('-', '_', $field);
            $value = date("H:i:s", strtotime($value));
        }

        $column = str_replace('-', '_', $field);
        $this->modality->{$column} = $value;
    }

    $this->modality->save();
    return redirect()->action('ModalityController@index');
}
    
06.07.2017 / 22:20
0

You can put the 'required' HTML attribute in your HTML. Thus, the field is always required.

Something like:

<input type="text" name="hora" required>

So the request will not be made to PHP if it is blank. You can also use Javascript + HTML masks to validate the format of the time entered, to prevent the user from entering something in writing or something.

In order to validate the inputs, you can get the $ input you received and go through them validating if the value is equal to '' (blank), if it is, you null arrow if it is an input of type 'time' therefore, if the value is null, it will be saved as worthless in the bank. To convert to array the input has the method toArray ()

    
06.07.2017 / 21:19