【问题标题】:Laravel relationship returning 1 for input valueLaravel 关系为输入值返回 1
【发布时间】:2015-08-04 11:05:13
【问题描述】:

我有一个文本输入:

{{ Form::text('first_name',  $car->basicCustomer->first_name or NULL, ['class' => 'form-control', 'disabled']) }}

应该返回名字,但输入的值是 1。

die 和 dumped 返回时的关系:

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 等也是如此。

有什么想法吗?

编辑:幸运地修复了错误,需要使用并检查关系是否在另一个视图中不为空:

{{ Form::text('first_name', (!$car->basicCustomer ? NULL : $car->basicCustomer->first_name), ['class' => 'form-control', 'disabled']) }}

现在输出名称,谁能解释原因:

或为空

没有工作?

【问题讨论】:

  • or NULL 被解释为条件表达式。它返回真 (1) 或假 (0)。 “任何字符串” OR NULL 返回真 (1)。

标签: php laravel-4 relationship


【解决方案1】:

or NULL 被解释为条件表达式。它返回 true (1) 或 false (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

【讨论】:

    猜你喜欢
    • 2016-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-31
    • 2014-08-21
    • 2016-06-21
    相关资源
    最近更新 更多