微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php-Laravel重用表单,有时存在变量

我有一个包含单独的日期和时间字段的表单,提交后,我的控制器实际上将这两个值结合起来存储到数据库中的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>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐