I have a project in which a number of models are defined using Eloquent. This is straightforward for the most part but I'm having trouble understanding how to handle a new case. I'm going to avoid using laravel/eloquent terminology in case I just obscure the problem.
I have a table called Question with fields id, QuestionTypeID, QuestionStatement.
Based on the QuestionTypeID a different class of question would need to be instantiated that may pull information for additional tables or use different business logic in the PHP code.
All of these question types could implement a common interface like:
interface Question {
int id;
string statement;
function ask($params);
// + CRUD stuff I suppose
}
How can I deal with having different Question models? Are Eloquent models not inherently tied to a particular table?
I have for example a SQLQuestion (where the logic would simply execute a stored SQL statement pulled from some lookup table and return the response) and a TimeFrameQuestion (whose logic requires calls to some TimeFrame modules).
The only table TimeFrameQuestion could be pulled from is the general Question table since it has no direct relation to any other table.
Is it possible to have 2 models both look at the Question table without the SQLQuestion collection also containing all the records that are of TimeFrameQuestion type and vice versa?
Question. Depending on how simple or complex it is, aQuestionInterfacemight be a good approach, and then you have differentQuestionmodels, each their own eloquent model that holds different information. - Scopeyprotected $tableon the fly.. and just a side note, in php you could add fields dynamically to classes.. not to mention those magic functions.. - Bagus Tesa