Laravel get data from relationship and show in view
<p>simply this code is my get data solution, this models are relationships and my result is true but i can't get data from relationship and show that on view, for example;</p>



<pre class="lang-php prettyprint-override"><code>$data = UserAccountNumber::with(['currency_type'])-&gt;orderBy('id', 'DESC')-&gt;paginate(50);
</code></pre>

<p>this code return below result:</p>

<pre class="lang-php prettyprint-override"><code>LengthAwarePaginator {#585 ▼
  #total: 2
  #lastPage: 1
  #items: Collection {#601 ▼
    #items: array:2 [▼
      0 =&gt; UserAccountNumber {#588 ▶}
      1 =&gt; UserAccountNumber {#589 ▼
        #table: "user_account_numbers"
        #guarded: array:1 [▶]
        #connection: null
        #primaryKey: "id"
        #perPage: 15
        +incrementing: true
        +timestamps: true
        #attributes: array:9 [▼
          "id" =&gt; 2
          "user_id" =&gt; 17
          "account_number" =&gt; "24234234"
          "card_number" =&gt; "242423423"
          "sheba" =&gt; "asdasdad"
          "currency_type" =&gt; 5
          "status" =&gt; 0
          "created_at" =&gt; "2016-05-13 10:55:00"
          "updated_at" =&gt; "2016-05-13 10:55:00"
        ]
        #original: array:9 [▶]
        #relations: array:1 [▼
          "currency_type" =&gt; CurrencyType {#603 ▼
            #table: "currency_type"
            #fillable: array:3 [▶]
            #dates: array:1 [▶]
            #connection: null
            #primaryKey: "id"
            #perPage: 15
            +incrementing: true
            +timestamps: true
            #attributes: array:7 [▼
              "id" =&gt; 5
              "user_id" =&gt; 1
              "currency_type" =&gt; "EURO"
              "currency_symbol" =&gt; "EUR"
              "created_at" =&gt; "2016-04-17 12:07:47"
              "updated_at" =&gt; "2016-04-17 12:07:47"
              "deleted_at" =&gt; null
            ]
</code></pre>

<p><strong>View:</strong></p>

<p>in view i can get <code>UserAccountNumber</code> data, but i can not get <code>currency_type</code> and show that on view:</p>

<pre class="lang-php prettyprint-override"><code>&lt;table class='table-style' cellpadding='0' cellspacing='0'&gt;
    @foreach ($data as $key=&gt;$contents)
        &lt;tr&gt;
            &lt;td&gt;{{$contents-&gt;card_number}}&lt;/td&gt;
            &lt;td&gt;{{$contents-&gt;card_number}}&lt;/td&gt;
            &lt;td&gt;{{$contents-&gt;seba}}&lt;/td&gt;
            &lt;td&gt;{{$contents-&gt;created_at}}&lt;/td&gt;
            &lt;td&gt;
                {{$contents-&gt;currency_type-&gt;currency_type}}
            &lt;/td&gt;
        &lt;/tr&gt;
    @endforeach
&lt;/table&gt;
</code></pre>

<p>i get error for <code>{{$contents-&gt;currency_type-&gt;currency_type}}</code> on view.</p>

<p><strong>Error:</strong></p>

<pre class="lang-php prettyprint-override"><code>Trying to get property of non-object
</code></pre>

<p><strong>POST UPDATED:</strong></p>

<pre class="lang-php prettyprint-override"><code>class UserAccountNumber extends Model
{
    protected $table   = 'user_account_numbers';
    protected $guarded = ['id'];

    public function user()
    {
        return $this-&gt;belongsTo('App\Http\Models\User');
    }

    public function currency_type()
    {
        return $this-&gt;belongsTo('App\Http\Models\CurrencyType', 'currency_type', 'id');
    }
}
</code></pre>