【发布时间】:2018-03-06 08:27:59
【问题描述】:
我得到了包含以下内容的数据透视表 帐户ID article_id created_at 更新时间
如何在 View 中检索所有这些。我有 2 个模型账号和文章
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();
}
}
我也有这样的控制器
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]);
}
}
现在我不使用 MVC 就这样检索它
@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
我想知道如何检索该枢轴中的所有列,以便在视图返回时显示该列中的所有数据。
【问题讨论】:
标签: php model laravel-5.4