0
I have an input field where the user type an hour (eg "08:00") convert this string with strtotime() and convert again with date() and saved in mysql in TIME format. So far, so good, although I think there’s a better way to do that. The problem is that if the input field is empty it is saved in the "00:00:00" bank and 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 you have a better way than using if in every 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>
If the user does not inform the data, in the database will be
NULL
?– Woss
What you can do is create a repeat loop that checks all values, but for that you would need to know exactly what your HTML form looks like.
– Woss
And what is the default for names? Only changes the value at the end? And is there any other field than those in the form?
– Woss
Yes, I said there is, but like I said, you need to know how the form is doing. I am not saying that he is wrong, but it is necessary to know what is the name of the fields. Without the name, how will I know which fields should I check? Just say if the names follow a pattern or not or if there are other fields besides these in the form. Without that information, there’s no way to answer you. So if you want help, please help.
– Woss