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>