在Laravel 4中使用数据透视表时如何分配关系?
Details
I have 3 tables :
- catalog_downloads
- export_frequencies
- export_frequencies_catalog_downloads (Pivot Table)
Diagram
I am not sure if I set the relation between them correctly. Please correct me if I am wrong.
Here is what I did
In CatalogDownload.php
public function export_frequencies(){
return $this->belongsToMany('ExportFrequency','export_frequencies_catalog_downloads','export_frequency_id','catalog_download_id');
}
In ExportFrequency.php
public function catalog_downloads(){
return $this->belongsToMany('CatalogDownload','export_frequencies_catalog_downloads','export_frequency_id','catalog_download_id');
}
Questions
According to my diagram - Did I assign the relationship correctly ? I hope I didn't mix up between
hasMany
andbelongsTo
Will I need a class or a model for a Pivot Table ?
Thanks
详细信息 h2>
我有3个表: p> \ n
- catalog_downloads li>
- export_frequencies li>
- export_frequencies_catalog_downloads(数据透视表) li>
ul>
图 h2>
p> \ n
我不确定我是否正确设置了它们之间的关系。 如果我错了,请纠正我。 p>
以下是我的工作 h2>
在
CatalogDownload.php code> p>
public function export_frequencies(){ return $ this-> belongsToMany('ExportFrequency','export_frequencies_catalog_downloads','export_frequency_id','catalog_download_id'); } code> pre>
在
ExportFrequency.php code> p>
public function catalog_downloads(){ return $ this-> belongsToMany('CatalogDownload', 'export_frequencies_catalog_downloads','export_frequency_id','catalog_download_id'); } code> pre>
问题 h2>
-
根据我的图表 - 我是否正确分配了关系? 我希望我没有在
hasMany code>和
belongsTo code> p> li>之间混淆
我是否需要课程或 数据透视表的模型? p> li> ul>
谢谢 p> div>
-
Since export_frequencies
is in the CatalogDownload
model you have to invert the ID's because the parameters of belongsToMany are as follows:
1. Name of the referenced (target) Model (ExportFrequency)
2. Name of the Pivot table
3. Name of the id colum of the referencing (local) Model (CatalogDownload in this case)
4. Name of the id colum of the referenced (target) Model (ExportFrequency in this case)
what leads to this function:
public function export_frequencies(){
return $this->belongsToMany('ExportFrequency','export_frequencies_catalog_downloads','export_frequency_id','catalog_download_id');
}
The other function was correct.
If you had some data in your pivot table, for instance a colum with the name someCounter
then you will have to tell the relation to load that column when creating the pivot object like this:
public function export_frequencies(){
return $this->belongsToMany('ExportFrequency','export_frequencies_catalog_downloads','export_frequency_id','catalog_download_id')->withPivot('someCounter');
}
That will load the column and make it avalible like this:
$catalogDownload->export_frequencies()->first()->pivot->someCounter;
You will need a separate Pivot Model if you need to do some special handling for the fields or if that pivot itself has a relation of its own but then you might consider using a full blown model instead of a pure Pivot Model.
As an added note to the accepted answer, you are able to set up your many to many relationships without referencing the pivot table and the relevant id's as long as you follow a specific convention.
You can name your pivot table using singular references to the related tables, like 'catalog_download_export_frequency'. Notice the alphabetic order of the singular references.
Then you can simply do:
// CatalogDownload Model
public function exportFrequencies()
{
return $this->belongsToMany('ExportFrequency');
}
// ExportFrequency Model
public function catalogDownloads()
{
return $this->belongsToMany('CatalogDownload');
}
This will then allow you to run queries using the query builder or Eloquent like:
$catalogDownload->exportFrequencies()->get(); // Get all export frequencies for a specific CatalogDownload.
Or
$this->catalogDownload->with('exportFrequencies')->find($id); // Using eager loading and dependency injection, when CatalogDownload is assigned to $this->catalogDownload
Hope this helps!