如何从数据透视表 table 查询数据并使用 blade 显示它?
How to query data from a pivot table and display it using blade?
我正在尝试从我的数据透视表中查询数据 table 并将其显示在我的视图中
我的观点
@foreach ($catalog_downloads as $catalog_download)
<td>
<?php $frequencies = $catalog_download->export_frequencies()->get(); ?>
{{{ $frequencies }}}
</td>
@endforeach
当我这样做时 {{{ $frequencies }}}
我得到了这个 :
[{"id":1,"name":"Semi Annual","created_at":"2015-01-14 15:44:59","updated_at":"2015-01-14 15:44:59","pivot":{"catalog_download_id":104,"export_frequency_id":1}},{"id":2,"name":"Quaterly","created_at":"2015-01-14 15:45:06","updated_at":"2015-01-14 15:45:06","pivot":{"catalog_download_id":104,"export_frequency_id":2}},{"id":3,"name":"Monthly","created_at":"2015-01-14 15:45:13","updated_at":"2015-01-14 15:45:13","pivot":{"catalog_download_id":104,"export_frequency_id":3}}]
但我不想这样。我只想要 name.
所以我做了 {{{ $frequencies->name or '' }}}
现在什么都没有了。 :(
更多详情
我是这样定义我的关系的。
public function export_frequencies(){
return $this->belongsToMany('ExportFrequency','export_frequency_catalog_download','catalog_download_id','export_frequency_id');
}
谁能告诉我我做错了什么?
默认情况下,Laravel 仅从主元table 中选择两个外键。要更改此设置,请将 withPivot
添加到您的关系定义中:
public function export_frequencies(){
return $this->belongsToMany('Frequency')->withPivot('name');
});
然后就可以通过pivot
object:
访问了
@foreach($frequencies as $frequency)
{{ $frequency->pivot->name }}
@endforeach
编辑
我想我被你的问题标题弄糊涂了。如果你只想显示来自 ExportFrequency
的数据,你不需要 withPivot
,你可以简单地这样做:
@foreach($frequencies as $frequency)
{{ $frequency->name }}
@endforeach
我正在尝试从我的数据透视表中查询数据 table 并将其显示在我的视图中
我的观点
@foreach ($catalog_downloads as $catalog_download)
<td>
<?php $frequencies = $catalog_download->export_frequencies()->get(); ?>
{{{ $frequencies }}}
</td>
@endforeach
当我这样做时 {{{ $frequencies }}}
我得到了这个 :
[{"id":1,"name":"Semi Annual","created_at":"2015-01-14 15:44:59","updated_at":"2015-01-14 15:44:59","pivot":{"catalog_download_id":104,"export_frequency_id":1}},{"id":2,"name":"Quaterly","created_at":"2015-01-14 15:45:06","updated_at":"2015-01-14 15:45:06","pivot":{"catalog_download_id":104,"export_frequency_id":2}},{"id":3,"name":"Monthly","created_at":"2015-01-14 15:45:13","updated_at":"2015-01-14 15:45:13","pivot":{"catalog_download_id":104,"export_frequency_id":3}}]
但我不想这样。我只想要 name.
所以我做了 {{{ $frequencies->name or '' }}}
现在什么都没有了。 :(
更多详情
我是这样定义我的关系的。
public function export_frequencies(){
return $this->belongsToMany('ExportFrequency','export_frequency_catalog_download','catalog_download_id','export_frequency_id');
}
谁能告诉我我做错了什么?
默认情况下,Laravel 仅从主元table 中选择两个外键。要更改此设置,请将 withPivot
添加到您的关系定义中:
public function export_frequencies(){
return $this->belongsToMany('Frequency')->withPivot('name');
});
然后就可以通过pivot
object:
@foreach($frequencies as $frequency)
{{ $frequency->pivot->name }}
@endforeach
编辑
我想我被你的问题标题弄糊涂了。如果你只想显示来自 ExportFrequency
的数据,你不需要 withPivot
,你可以简单地这样做:
@foreach($frequencies as $frequency)
{{ $frequency->name }}
@endforeach