2016-05-13 6 views
1

einfach dieser Code ist mein Get Data Lösung, diese Modelle sind Beziehungen und mein Ergebnis ist wahr, aber ich kann nicht Daten aus Beziehung und zeigen, dass auf Sicht, zum Beispiel;Laravel erhalten Daten aus Beziehung und zeigen in Sicht

$data = UserAccountNumber::with(['currency_type'])->orderBy('id', 'DESC')->paginate(50); 

dieser Code Rückkehr unten Ergebnis:

LengthAwarePaginator {#585 ▼ 
    #total: 2 
    #lastPage: 1 
    #items: Collection {#601 ▼ 
    #items: array:2 [▼ 
     0 => UserAccountNumber {#588 ▶} 
     1 => UserAccountNumber {#589 ▼ 
     #table: "user_account_numbers" 
     #guarded: array:1 [▶] 
     #connection: null 
     #primaryKey: "id" 
     #perPage: 15 
     +incrementing: true 
     +timestamps: true 
     #attributes: array:9 [▼ 
      "id" => 2 
      "user_id" => 17 
      "account_number" => "24234234" 
      "card_number" => "242423423" 
      "sheba" => "asdasdad" 
      "currency_type" => 5 
      "status" => 0 
      "created_at" => "2016-05-13 10:55:00" 
      "updated_at" => "2016-05-13 10:55:00" 
     ] 
     #original: array:9 [▶] 
     #relations: array:1 [▼ 
      "currency_type" => 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" => 5 
       "user_id" => 1 
       "currency_type" => "EURO" 
       "currency_symbol" => "EUR" 
       "created_at" => "2016-04-17 12:07:47" 
       "updated_at" => "2016-04-17 12:07:47" 
       "deleted_at" => null 
      ] 

Ausblick:

im Hinblick i UserAccountNumber Daten bekommen, aber ich kann currency_type nicht bekommen und dass auf Sicht zeigen:

<table class='table-style' cellpadding='0' cellspacing='0'> 
    @foreach ($data as $key=>$contents) 
     <tr> 
      <td>{{$contents->card_number}}</td> 
      <td>{{$contents->card_number}}</td> 
      <td>{{$contents->seba}}</td> 
      <td>{{$contents->created_at}}</td> 
      <td> 
       {{$contents->currency_type->currency_type}} 
      </td> 
     </tr> 
    @endforeach 
</table> 

Ich bekomme Fehler für {{$contents->currency_type->currency_type}} auf Sicht.

Fehler:

Trying to get property of non-object 

POST AKTUALISIERT:

class UserAccountNumber extends Model 
{ 
    protected $table = 'user_account_numbers'; 
    protected $guarded = ['id']; 

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

    public function currency_type() 
    { 
     return $this->belongsTo('App\Http\Models\CurrencyType', 'currency_type', 'id'); 
    } 
} 
+0

Can Sie zeigen uns bitte Ihr 'UserAccountNumber' Modell? – codedge

+0

@codedge jawohl. Beitrag aktualisiert. –

+0

Ich kann immer noch keine 'class UserAccountNumber' Modellklasse sehen. – codedge

Antwort

0

Das erste, was zu verbessern in Ihrem UserAccountNumber Modell. Ihre currency_type Beziehung sollte wie geschrieben werden:

public function currencyType() 
{ 
    return $this->belongsTo(CurrencyType::class, 'currency_type', 'id'); 
} 

Ihr Anruf würde dann wie folgt aussehen:

// using the currencyType() method from above 
$data = UserAccountNumber::with('currencyType') 
    ->orderBy('id', 'DESC') 
    ->paginate(50); 

Sie dann die Währung in Ihrem zugreifen können mit mit:

@foreach ($data as $key=>$contents) 
    $contents->currencyType->currency_type 
@endforeach 
0

Es scheint einige der UserAccountNumber haben nicht currency_type daran befestigt. Fügen Sie etwas zu Ihrem Code:

@if (isset($contents->currency_type)) 
    {{ $contents->currency_type->currency_type }} 
@endif 

Oder:

{{ $contents->currency_type->currency_type or 'There is not type' }} 
+0

'currency_type' ist eine Beziehung –

+0

Aber wenn es eine richtige Beziehung ist, ich. e. One-to-Many, ein CurrencyType-to-many-UserAccountNumbers, es sollte immer einen CurrencyType geben, richtig @Alexey? – codedge