Laravel 关系为输入值返回 1
Laravel relationship returning 1 for input value
我有一个文本输入:
{{ Form::text('first_name', $car->basicCustomer->first_name or NULL, ['class' => 'form-control', 'disabled']) }}
应该 return 名字,但输入的值为 1。
die和dump的关系returns:
object(Customer)#805 (20) {
["connection":protected] "main_site"
["fillable":protected] array(8) {
[0] "title"
[1] "first_name"
[2] "last_name"
[3] "email"
[4] "telephone_number"
[5] "address"
[6] "post_code"
[7] "company_name"
}
["table":protected] NULL
["primaryKey":protected] "id"
["perPage":protected] 15
["incrementing"] true
["timestamps"] true
["attributes":protected] array(3) {
["first_name"] "John"
["last_name"] "Doe"
["id"] "19854"
}
["original":protected] array(3) {
["first_name"] "John"
["last_name"] "Doe"
["id"] "19854"
etc...
输入中的值始终为 1,即使调用 last_name 等
有什么想法吗?
编辑:幸运地修复了错误,需要使用 if 来检查关系在另一个视图中是否不为空:
{{ Form::text('first_name', (!$car->basicCustomer ? NULL : $car->basicCustomer->first_name), ['class' => 'form-control', 'disabled']) }}
现在输出名称,谁能解释为什么:
或 NULL
没有工作?
将or NULL
解释为条件表达式。它 return 真 (1) 或假 (0)。
'test' or null :
boolean true
'' or null :
boolean false
true or null :
boolean true
false or null :
boolean false
在您的情况下,您可以使用 ?:
运算符。
$string ?: null
哪个是
的较短版本
$string ? $string : null
我有一个文本输入:
{{ Form::text('first_name', $car->basicCustomer->first_name or NULL, ['class' => 'form-control', 'disabled']) }}
应该 return 名字,但输入的值为 1。
die和dump的关系returns:
object(Customer)#805 (20) {
["connection":protected] "main_site"
["fillable":protected] array(8) {
[0] "title"
[1] "first_name"
[2] "last_name"
[3] "email"
[4] "telephone_number"
[5] "address"
[6] "post_code"
[7] "company_name"
}
["table":protected] NULL
["primaryKey":protected] "id"
["perPage":protected] 15
["incrementing"] true
["timestamps"] true
["attributes":protected] array(3) {
["first_name"] "John"
["last_name"] "Doe"
["id"] "19854"
}
["original":protected] array(3) {
["first_name"] "John"
["last_name"] "Doe"
["id"] "19854"
etc...
输入中的值始终为 1,即使调用 last_name 等
有什么想法吗?
编辑:幸运地修复了错误,需要使用 if 来检查关系在另一个视图中是否不为空:
{{ Form::text('first_name', (!$car->basicCustomer ? NULL : $car->basicCustomer->first_name), ['class' => 'form-control', 'disabled']) }}
现在输出名称,谁能解释为什么:
或 NULL
没有工作?
将or NULL
解释为条件表达式。它 return 真 (1) 或假 (0)。
'test' or null :
boolean true
'' or null :
boolean false
true or null :
boolean true
false or null :
boolean false
在您的情况下,您可以使用 ?:
运算符。
$string ?: null
哪个是
的较短版本$string ? $string : null