0

I have an intermediary table 'tour_tourists_documents'(i've also createad a model for it):

tour_id | tourist_id | doc_1 | doc_2

doc_1 and doc_2 relate to 'id' in table "Documents".

I want to set a relationship which retrieves both doc_1 and doc_2 from Documents. Smth like:

Class Tour_Tourist_Documents {

public function documents () {

 return $this->hasMany('App\Documents', 'id', 'doc_1' AND 'id', 'doc_2');

}

so I want this method to return a collection of document models corresponding to doc_1 and doc_2. and preserving their relationship to 'Tour_tourist_documents" model.

I've tried to do it like Collin James advised here: Laravel 5 hasMany relationship on two columns But it returns:

LogicException with message 'Relationship method must return an object of type Illuminate\Database\Eloquent\Relations\Relation'

Sergej Fomin
  • 1,822
  • 3
  • 24
  • 42
  • I think this is more of an issue with database design than Laravel relationships; currently you'd have to define `documentOne()` and `documentTwo()` relationships and merge them somehow. If you changed your schema to have a single `documents` table and linked it to tours or tourists via some kind of pivot table, you'd simply define a `documents()` function on each model, which would return as many documents as are attached. – Tim Lewis Sep 18 '17 at 18:35
  • Hi, Tim. 'tour_tourists_documents' is actually a pivot for which shows which tourists and their documents belong to a tour. – Sergej Fomin Sep 18 '17 at 20:52
  • Right and I get that, but say if you changed `doc_1` to `document_id` and linked that to a `documents` table, you'd have an easier time querying and wouldn't be limited to just 2 documents. Just a suggestion though; I'm not sure of a good approach with your current schema, which is why I left this all as a comment. – Tim Lewis Sep 18 '17 at 20:55

0 Answers0