Laravel 中的 isset(Input::old('abc')

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18839890/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 08:26:05  来源:igfitidea点击:

isset(Input::old('abc') in laravel

phplaravel

提问by 1myb

The code i used in the something.blade.php is

我在 something.blade.php 中使用的代码是

{{ Form::text('fullname', isset(Input::old('fullname'))?Input::old('fullname'):$hello[1] }}

but i not sure why it will return me the following error

但我不确定为什么它会返回以下错误

Can't use function return value in write context

不能在写上下文中使用函数返回值

Been trying with isset, trim, empty but nothing could work. What's the problem?

一直在尝试使用isset,trim,empty,但没有任何效果。有什么问题?

采纳答案by Connor Peet

You have not closed your parenthesis:

你没有关闭你的括号:

{{ Form::text('fullname', Input::old('fullname') ? Input::old('fullname') : $hello[1]) }}

Also, the input will return a null (not undefined), and I've changed that as well.

此外,输入将返回一个空值(不是未定义的),我也改变了它。

回答by The Alpha

You can simply use this

你可以简单地使用这个

{{ Form::text('fullname', Input::old('fullname', $hello[1] ?: '' ) }}

Input::old()takes a default value, optionally. If the old value is available then old value will be used other the deafult value.

Input::old()可选地采用默认值。如果旧值可用,则将使用旧值而不是默认值。

回答by fideloper

issetis weird like that. You can't use a function in it, as its not for testing if a function returns a value but rather if a variable is actually set.

isset就这么奇怪。您不能在其中使用函数,因为它不是用于测试函数是否返回值,而是用于测试是否实际设置了变量。

The simplest solution is:

最简单的解决方案是:

if(Input::old('fullname'))

That will return nullif fullname isn't set.

null如果未设置全名,那将返回。

Alternatively :

或者 :

$old = Input::old();
isset($old['fullname']) ...

(I suggest passing in variables into your view from your controller instead of using function calls in the view (with some exceptions of course)

(我建议将变量从控制器传递到视图中,而不是在视图中使用函数调用(当然有一些例外)

回答by user3779015

You can add second parameter to be the default value or you can place conditional on it.

您可以将第二个参数添加为默认值,也可以对其设置条件。

example:

例子:

old('field_name', 'default)or old('field_name', $user ? $user->field_name : '' )

old('field_name', 'default)或者 old('field_name', $user ? $user->field_name : '' )