iori
iori

Reputation: 3506

How to assign relation when working with pivot table in Laravel 4?

Details

I have 3 tables :

Diagram

enter image description here

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

Thanks

Upvotes: 0

Views: 153

Answers (2)

Gareth Daine
Gareth Daine

Reputation: 4186

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!

Upvotes: 1

ITroubs
ITroubs

Reputation: 11215

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.

Upvotes: 2

Related Questions