php-Laravel重用表单,有时存在变量
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php-Laravel重用表单,有时存在变量,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2047字,纯文字阅读大概需要3分钟。
内容图文
![php-Laravel重用表单,有时存在变量](/upload/InfoBanner/zyjiaocheng/663/80a06400b1cc41fbb44c4ae415bedfff.jpg)
我有一个包含单独的日期和时间字段的表单,提交后,我的控制器实际上将这两个值结合起来存储到数据库中的datetime字段中.
形成:
<div class="form-group @if ($errors->has('date')) has-error @endif">
{!! Form::label('date', 'Date') !!}
{!! Form::date('date', null, ['class' => 'form-control']) !!}
<small class="text-danger">{{ $errors->first('date') }}</small>
</div>
<div class="form-group @if ($errors->has('time')) has-error @endif">
{!! Form::label('time', 'Time') !!}
{!! Form::time('time', null, ['class' => 'form-control']) !!}
<small class="text-danger">{{ $errors->first('time') }}</small>
</div>
在创建新记录时,这可以很好地工作,但是我想在编辑页面上重复使用表单(使用相同的部分),我必须像这样编辑输入中的值:
<div class="form-group @if ($errors->has('date')) has-error @endif">
{!! Form::label('date', 'Date') !!}
{!! Form::date('date', $booking->reservation_datetime->format('Y-m-d'), ['class' => 'form-control']) !!}
<small class="text-danger">{{ $errors->first('date') }}</small>
</div>
<div class="form-group @if ($errors->has('time')) has-error @endif">
{!! Form::label('time', 'Time') !!}
{!! Form::time('time', $booking->reservation_datetime->format('G:i'), ['class' => 'form-control']) !!}
<small class="text-danger">{{ $errors->first('time') }}</small>
</div>
但这将因此在创建页面上引起问题.
如何使用相同的表单,但仅在编辑页面上加载值?
解决方法:
您可以使用中间变量:
<?php $date = isset($booking->reservation_datetime) ? $booking->reservation_datetime->format('Y-m-d') : null ?>
<div class="form-group @if ($errors->has('date')) has-error @endif">
{!! Form::label('date', 'Date') !!}
{!! Form::date('date', $date, ['class' => 'form-control']) !!}
<small class="text-danger">{{ $errors->first('date') }}</small>
</div>
<?php $time = isset($booking->reservation_datetime) ? $booking->reservation_datetime->format('G:i') : null ?>
<div class="form-group @if ($errors->has('time')) has-error @endif">
{!! Form::label('time', 'Time') !!}
{!! Form::time('time', $time, ['class' => 'form-control']) !!}
<small class="text-danger">{{ $errors->first('time') }}</small>
</div>
内容总结
以上是互联网集市为您收集整理的php-Laravel重用表单,有时存在变量全部内容,希望文章能够帮你解决php-Laravel重用表单,有时存在变量所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。