2

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...

3 Answers3

0

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!

parker_codes
  • 3,267
  • 1
  • 19
  • 27
  • Ok thank you, but with this foreach loop, all attributes showed and I want to show NAME only. if($key == "NAME") { echo $value; } this method is enough? Or is there another way without using an if condition ? – yagrasdemonde Feb 06 '19 at 08:44
  • @yagrasdemonde, see my edit regarding the null-coalesce operator. Forget the foreach loop and access it directly using the example. – parker_codes Feb 06 '19 at 15:40
0

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

-1

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.

Michael Miller
  • 399
  • 1
  • 9