lundi 25 juillet 2016

Trying to get property of non-object in Foreach loop: Laravel 5.2

I have following code.

@foreach($Countries as $Country)
    {!! $Country->Currency->Currency !!}
@endforeach

It shows error : Trying to get property of non-object on this line $Country->Currency->Currency

When I print the value using below code

@foreach($Countries as $Country)
    {!! dd($Country->Currency); !!}
@endforeach

It show value. It is below

CurrencyModel {#320 ▼
  +table: "tblcurrency"
  +primaryKey: "CurrencyID"
  +timestamps: true
  #connection: null
  #keyType: "int"
  #perPage: 15
  +incrementing: true
  #attributes: array:7 [▼
    "CurrencyID" => 1
    "Currency" => "INR"
    "CountryID" => 1
    "Symbol" => "₹"
    "IsActive" => 0
    "created_at" => "2016-07-25 06:36:42"
    "updated_at" => "2016-07-25 06:39:52"
  ]
  #original: array:7 [▶]
  #relations: []
  #hidden: []
  #visible: []
  #appends: []
  #fillable: []
  #guarded: array:1 [▶]
  #dates: []
  #dateFormat: null
  #casts: []
  #touches: []
  #observables: []
  #with: []
  #morphClass: null
  +exists: true
  +wasRecentlyCreated: false
}

But, when I try like below

@foreach($Countries as $Country)
    {!! $Country->Currency->Currency !!}
@endforeach

I get this error: Trying to get property of non-object

Am I missing something?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire