oliverbusk's avatar

Relationship belongsTo two columns

I have three models:

  1. File
  2. Field
  3. Result

A file can have many fields, and a field can belong to many files. This gives me:

Files.php

public function fields()
{
    return $this->hasMany(Field::class);
}

Fields.php

public function files()
{
    return $this->belongsToMany(Files::class);
}

Now, my users can upload files to my website and create fields. All files uploaded will "inherit" the fields.

A field could, for example, be called Invoice Number. My user can then add the invoice number from the uploaded file to the field.

However, as each uploaded file is unique, this means that the final field content will also be unique, thus requiring me to create a third model result.

I am not sure how to define this relationship because a result belongs to a field and a file.

Example

files

id | name
1  | invoice1.pdf
2  | invoice2.pdf

fields

id | name
1  | Invoice Number

results

id | file_id | field_id | content
1  | 1       | 1        | #1234
2  | 2       | 1        | #8888

I would then like to be able to get the result for the field, for a specific file, such as:

Filename = invoice1.pdf Fields = Invoice Number Results = #1234

0 likes
1 reply
DavidPetrov's avatar
Level 2

There's a ready conception for that purpose, you don't need that third model. What you need is a two sided many to many relationship with a pivot table attribute. The third table results you have shown is exactly 1:1 the pivot table of the file-field many to many relationship. Check out the documentation: https://laravel.com/docs/5.8/eloquent-relationships#many-to-many you'll find examples. But in general what you'd need is: File.php

public function fields()
{
    return $this->belongsToMany(Field::class)->withPivot('content');
}

Field.php

public function files()
{
    return $this->belongsToMany(File::class)->withPivot('content');
}

whereas the pivot table (conventionally named field_file) must contain the following columns as you've shown:

id | file_id | field_id | content
1  | 1       | 1        | #1234
2  | 2       | 1        | #8888

Please or to participate in this conversation.