2017-09-25 1 views
0

Ich habe Pivot-Tabelle, die bestehen aus ACCOUNT_ID article_id created_at updated_atwie alle Wert in Pivot-Tabelle zurück in Laravel 5.4

wie alle von ihnen in Aussicht abzurufen. Ich habe 2 Modell Konto und Artikel

class Account extends Model 
{ 
    public function articles() 
    { 
     return $this->belongsToMany('App\Article','accountsarticles')->withPivot('account_id','article_id')->withTimestamps(); 
    } 
} 

class Article extends Model 
{ 
    public function accounts() 
    { 
     return $this->belongsToMany('App\Account','accountsarticles')->withPivot('account_id','article_id')->withTimestamps(); 
    } 
} 

Und ich habe auch Controller wie diese

class AccountsArticlesController extends Controller 
{ 
    public function index() 
    { 
     /*If i use this 1 i can get all of column data*/ 
     $accountsarticles=DB::table('accountsarticles')->get(); 

     /*If i use this 1 i only get created_at and updated_at*/ 
     $acc = Account::all(); 
     return view('accountsarticles.index',['accountsarticles'=>$accountsarticles]); 
    } 
} 

Vorerst ich es wie folgt abrufen, ohne MVC mit

@foreach($accountsarticles as $article) 
    <tr> 
     <td>{{$article->created_at}}</td> 
     <td>{{$article->updated_at}}</td> 
     <td>{{$article->account_id}}</td> 
     <td>{{$article->article_id}}</td> 
    </tr> 
@endforeach 

Ich frage mich, wie abrufen Alle Spalten in diesem Pivot, damit ich alle Daten in dieser Spalte anzeigen kann, wenn die Ansicht zurückgegeben wird.

Antwort

0

die folgende Syntax $model->pivot->field_name

In Ihrem Fall verwenden, wird es sein:

//to fetch account id 
$article->pivot->account_id 

//to fetch article id 
$article->pivot->article_id 
+0

ich das schon versuchen, aber es gibt mir „Eigentum von Nicht-Objekt erhalten Trying“ – Student

+0

Sie sind nicht Senden einer Sammlung von Modellen aus Ihrer Sicht. Sie verwenden 'DB', um Daten nur aus dem Tabellennamen zu holen' $ accountsarticles = DB :: table ('accountsarticles') -> get() 'deshalb gibt es einen Fehler. –

+0

@Student senden '$ acc = Account :: all()' Daten an die Ansicht. –