Tenho um campo input em que o usuário digita uma hora (ex: "08:00") converto essa string com o strtotime() e converto novamente com date() e salvo no mysql no formato TIME. Até ai está tudo certo, apesar de achar que deve ter uma forma melhor de fazer isso. O problema é que se o campo input estiver vazio ele salva no banco "00:00:00" sendo que não deveria salvar nada. Se fizer um if (if($sfi1)) para saber se o strtotime retorna false e usar o date() somente se existir algo, tudo funciona corretamente. Mas quero saber se tem uma forma melhor do que usar if em cada 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');
}
Formulário:
<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>
NULL
?