在Laravel中编辑表单时显示旧值的最佳实践是什么? [英] What is the best practice to show old value when editing a form in Laravel?

查看:69
本文介绍了在Laravel中编辑表单时显示旧值的最佳实践是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写编辑表单的逻辑,在输入中显示数据时会遇到一些麻烦.

I am writing the logic for an edit form and have some complications when displaying data in the inputs.

最初显示表单时,显示的记录值如下:

When I initially show the form, I show the records values like:

value="{{$dog->title}}"

然后,当表单未通过验证时,我需要显示旧输入,以便用户不会丢失他已经输入的内容.因此,我需要一种方法来显示旧数据,例如:

Then when the form does not pass the validation I need to show the old input, so that user does not loose what he has already input. So I need to have a way to display old data like:

value="{{old('title')}}"

由于我需要输入旧数据以防万一,因此我得到了以下代码:

Because I need to input old data in case it exists, I ended up with this code:

value="{{$dog->title or old('title')}}"

然后在控制器中检查Request是否具有旧输入,我为$ dog var分配一个空值.

And in controller I check if Request has old input, I assign the $dog var a null value.

我想问一问,这是否被认为是可以接受的做法,或者是否有更好且正确"的方式来做?

I wanted to ask if that is considered an OK practice or is there a better and 'correct' way to do it?

推荐答案

函数 old 如果在会话中未找到旧数据,则具有默认参数.

Function old have default parameter if no old data found in session.

function old($key = null, $default = null)

您可以将模板中的表达式替换为

You can replace expression in template with

value="{{old('title', $dog->title)}}"

这篇关于在Laravel中编辑表单时显示旧值的最佳实践是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆