dongzun9958
2019-02-05 20:24
浏览 266
已采纳

获取Collection内的Object值

I use DataTables as Service from Yajra in my Laravel application.

I have a collection like this :

Collection {#1178
    #items: array:2 [
    0 => array:7 [
      "email" => "user1@example.com"
      "id" => 6
      "emailBlacklisted" => false
      "smsBlacklisted" => false
      "modifiedAt" => "2019-02-05T17:20:17.723+01:00"
      "listIds" => array:2 [
        0 => 2
        1 => 3
      ]
      "attributes" => {#1139
        +"NAME": "Doe"
        +"FIRSTNAME": "John"
      }
    ]
    1 => array:7 [
      "email" => "user2@example.com"
      "id" => 1
      "emailBlacklisted" => false
      "smsBlacklisted" => false
      "modifiedAt" => "2019-02-05T21:12:04.094+01:00"
      "listIds" => array:1 [
        0 => 2
      ]
      "attributes" => {#1143}
    ]
  ]
}

In my blade view I show email value with {{ $email }} -> Simple

I think this is a very easy problem for you ...

But I can't display the value of attributes key. (I want to show the NAME : Doe). -> attributes is an object inside my collection.

Thank you for helping me unlock...

图片转代码服务由CSDN问答提供 功能建议

我在Laravel应用程序中使用DataTables作为Yajra的服务。

I 有这样的集合:

  Collection {#1178 
 #items:array:2 [
 0 0 => 数组:7 [
“电子邮件”=>  “user1@example.com”
“id”=>  6 
“emailBlacklisted”=>  false 
“smsBlacklisted”=>  false 
“modifiedAt”=>  “2019-02-05T17:20:17.723 + 01:00”
“listIds”=> 数组:2 [
 0 =>  2 
 1 =>  3 
] 
“attributes”=>  {#1139 
 +“NAME”:“Doe”
 +“FIRSTNAME”:“John”
} 
] 
 1 => 数组:7 [
“电子邮件”=>  “user2@example.com”
“id”=>  1 
“emailBlacklisted”=>  false 
“smsBlacklisted”=>  false 
“modifiedAt”=>  “2019-02-05T21:12:04.094 + 01:00”
“listIds”=> 数组:1 [
 0 =>  2 
] 
“attributes”=>  {#1143} 
] 
] 
} 
   
 
 

在我的刀片视图中,我使用{{$ email}} - >显示电子邮件值 简单

我认为这对你来说是一个非常容易的问题...

但是我无法显示属性键的值。 (我想展示NAME:Doe)。 -> 属性是我的集合中的一个对象。

感谢您帮我解锁...

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dongxili9934 2019-02-06 03:45
    已采纳

    If I'm understanding correctly, you want to be able to display the key and the value of the attributes.

    If you are utilizing blade, you could try an expanded foreach loop:

    @foreach($attributes as $key => $value)
        {{ $key }}: {{ $value }}
    @endforeach
    

    This assumes that you already have access to the attributes on each individual model, such as $item->email or in this case $item->attributes. If you need to, you can do @foreach($item->attributes as $key => $value) to start it off.


    If you are only looking to display a specific value, use the null-coalesce operator ??.

    $item->attributes['NAME'] ?? ''
    

    You could use this in your logic elsewhere with any expression that might be null:

    // the fallback does not have to be a string
    $person = Person::find($id) ?? Person::first();
    
    // it can be chained
    $value = $parameter ?? $localDefault ?? $globalDefault;
    

    If the NAME is not found, it will fall back to what comes after the ??, which is an empty string in the example above. This is a nice trick to avoid any errors if the attribute doesn't exist. It is doing the same thing as a ternary checking if it is null:

    ($item->attributes['NAME'] !== null) ? $item->attributes['NAME'] : '';
    

    That is obviously messy, so the null-coalesce operator comes in handy!

    已采纳该答案
    打赏 评论
  • dongre6227 2019-02-05 20:38

    You should simply do something like:

    @foreach($collection as $item)
      {{$item->NAME}}
    @endforeach
    

    Note: NAME must be a key in the attributes variable.

    The attributes variable is protected, so you can't reference it directly from outside of the object. The value will be automatically mapped if you reference it through the object that owns it.

    打赏 评论
  • duanmu5641 2019-02-07 12:21

    Ok so, @GoogleMac put me on the track. In fact, as the NAME attribute is not always present, I have to test the variable with an

        isset()
    

    function and not

        !== NULL
    

    With

        {{ isset($attributes->NOM) ? $attributes->NAME : 'NC' }}
    

    The code works very well.

    Thank you @GoogleMac and @Davit

    打赏 评论

相关推荐 更多相似问题