1

I'm having trouble retrieving a certain set of results with my Eloquent relations.

Models:
Application(table: application) [id, title, active]
Question(table: question) [id, application_id, question_text, helper_text, question_type]
QuestionType(table: question_type) [id, type]
Bold = primary key, italic = foreign key

Relationships:
One Application has many Questions.
Many Questions can belong to one Application.

One Question has one QuestionType (referenced by question_type FK)
One QuestionType can belong to many Questions (by referencing it's id as a question_type)

QuestionType is a static table that will never have values added or removed, however the type attributes can change.

I would like to be able do something like this:

$application = Application:find($application_id);  
$questions = $application->questions()->get();

and have that replace question_type with the appropriate type pulled from the QuestionType model.

I have look through the Laravel and Eloquent docs, asked on IRC, and looked through other StackOverflow articles, yet could not find an answer that helped. I think what is throwing me off is my unconventional foreign key, question_type, in my Question table. I got it to KIND OF work once, exception question_type was being replaced with an QuestionType array (which won't work).

2
  • Can you call your field question_type_id instead? That might eliminate some confusion and make things work a little more naturally.
    – mopo922
    Jan 27, 2015 at 21:42
  • I could, but the thing is I need that question_type for other purposes (mostly working with decision logic). Would it be possible to just tack the "type" onto the Question results instead of replacing question_type with it? Jan 27, 2015 at 21:45

1 Answer 1

2

Id say first add this relationship to Question:

public function type(){
   return $this->belongsTo('QuestionType', 'question_type');
}

Then just use it like normal:

$question = Question::find(1);
$question->type;

And for all questions of an application (with eager loading)

$application = Application:find($application_id);  
$questions = $application->questions()->with('type')->get();

Edit

To get only the actual name of the type (column named type) you can add an attribute accessor. However with that the naming gets a bit more difficult. If you really don't want to change your foreign key name to question_type_id I suggest this:

public function typeRelation(){
    return $this->belongsTo('QuestionType', 'question_type');
}

public function getTypeAttribute(){
    if(is_null($this->typeRelation)) return null;
    return $this->typeRelation->type;
}

The attribute accessor allows you to use $question->type and get the attribute of the related model directly. Don't forget to adjust the relation name when eager loading: with('typeRelation')

10
  • I like this, it eliminates the naming conflict between property and relationship.
    – mopo922
    Jan 27, 2015 at 21:45
  • 2
    Yes. Also, in my opinion, having $question->questionType is quite redundant. I like $question->type a lot more :) Jan 27, 2015 at 21:46
  • Ah, that's a lot closer! Now it returns this (by iterating through the collection): {"id":2,"type":"Short Answer","created_at":"2015-01-26 20:33:29","updated_at":"2015-01-26 20:33:29"} Is there a way I can access it like $q->type and just receive "Short Answer"? Jan 27, 2015 at 21:50
  • Isn't that what you wanted? Or do you only want the actual name? (type) Jan 27, 2015 at 21:51
  • 2
    Add a check for null in the accessor. It might not be necessary in this very case (I suppose a question cannot exist without a type) but it's a good practice to save yourself a headache one day, when you loop through the questions and there's one without related model. Jan 27, 2015 at 23:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.