我对关系数据库比较陌生,因此我想出一种方法来从特定用户那里获取当天调查的答复。我的最终目标是检查用户在调查中走了多远,或者用户是否已完成该调查。感谢您的帮助。
我有一个参与者表,一个调查表,一个调查问题表和一个调查答复表。
每个参与者都有很多调查
每个参与者对调查问题都有很多回答(2)
每个调查都有很多问题
每个问题都有一个答案
我不确定是否应该建立“ hasManyThrough”关系。到目前为止,使用我的模型,我可以找到参与者,然后找到相应的调查,然后从该调查中收集问题。这是链条断裂的地方,我无法使用从调查中检索到的问题,也无法使用我的“ SurveyResponse”模型找到他们的答案。有人可以帮我弄清楚我的数据库结构/关系是否正确吗?
我的控制器(我可以到达它回显$ question-> body的位置,但是我无法使用$ question-> responses()-> get之类的东西来检查对问题的响应):< / p>
public function findResponses(Request $request)
{
$participant_id = '1234';
$surveyDate = new Carbon();
$surveyDate = $surveyDate->toDateString();
$participant = Participant::where('user_id', $participant_id)->first();
// check if participant daily survey is initiated
$surveyStarted = $participant->dailySurveyInitiated;
if($surveyStarted)
{
$activeSurveyParticipant = Participant::where('user_id', $participant_id)->first();
// find survey for that date corresponding to that user
$activeSurvey = Survey::where('survey_Date', $surveyDate)->where('participant_id', $participant_id)->first();
$surveyID = $activeSurvey->id;
// gather the questions
$activeSurveyQuestions = $activeSurvey->SurveyQuestions()->where('survey_id', '=', $surveyID)->get();
// check to see what questions have been answered
foreach($activeSurveyQuestions as $question)
{
echo $question->body;
echo '<br>';
// really want to gather the responses to each
// question based off the participant_id...
// something like
// $currentQ = $question->SurveyResponse::where('response', '!=', null)->get();
// echo $currentQ->response;
}
}
else
{
}
}
我的参与者模型:
class Participant extends Model
{
protected $table = 'participants';
protected $primaryKey = "user_id";
public function surveys()
{
return $this->hasMany('App\Survey', 'participant_id', 'user_id');
}
public function surveyQuestionResponses()
{
return $this->hasMany('App\Survey', 'App\SurveyQuestionResponses');
}
public function surveyResponses()
{
return $this->hasManyThrough('App\SurveyResponse', 'App\SurveyQuestion');
}
}
参与者表:
class CreateParticipantsTable extends Migration
{
protected $primaryKey = 'user_id';
protected $incrementing = false;
public function up()
{
Schema::create('participants', function (Blueprint $table)
{
$table->engine = 'InnoDB';
$table->string('user_id')->unique();
$table->boolean('subscribed')->default(0);
$table->boolean('dailySurveyInitiated')->default(0)->nullable();
$table->integer('dailySurveyCompleted')->default(0)->nullable();
$table->boolean('studyCompleted')->default(0);
$table->timestamps();
});
}
我的调查模型:
class Survey extends Model
{
protected $table = 'surveys';
public function SurveyQuestions()
{
return $this->hasMany('App\SurveyQuestion');
}
// Participant
public function participant()
{
return $this->belongsToMany('App\Participant', 'user_id', 'participant_id');
}
}
我的调查表:
class CreateSurveysTable extends Migration
{
public function up()
{
Schema::create('surveys', function (Blueprint $table)
{
$table->engine = 'InnoDB';
$table->increments('id');
$table->string('participant_id')->nullable();
$table->string('survey_date')->nullable();
$table->boolean('completed')->nullable()->default(0);
$table->timestamps();
});
Schema::table('surveys', function($table) {
$table->foreign('participant_id')->references('user_id')->on('participants');
});
}
}
我的SurveyQuestions模型:
class SurveyQuestion extends Model
{
protected $table = 'survey_questions';
public function survey()
{
return $this->belongsTo('App\Survey', 'survey_id');
}
public function participant()
{
return $this->belongsTo('App\Participant', 'participant_id');
}
public function responses()
{
return $this->hasMany('App\SurveyResponse', 'question_id');
}
}
我的SurveyQuestions表:
class CreateSurveyQuestionsTable extends Migration
{
public function up()
{
Schema::create('survey_questions', function (Blueprint $table)
{
$table->engine = 'InnoDB';
$table->increments('id');
$table->string('body')->nullable();
$table->enum('type', ['numeric', 'image'])->nullable();
$table->integer('survey_id')->nullable();
$table->timestamps();
});
Schema::table('survey_questions', function($table) {
$table->foreign('survey_id')->references('id')->on('surveys');
});
}
}
我的SurveyResponse模型:
class SurveyResponse extends Model
{
protected $table = 'survey_responses';
public function question()
{
return $this->belongsTo('App\SurveyQuestion');
}
public function participant()
{
return $this->belongsTo('App\Participant');
}
}
我的SurveyResponse表:
class CreateSurveyResponsesTable extends Migration
{
public function up()
{
Schema::create('survey_responses', function (Blueprint $table)
{
$table->engine = 'InnoDB';
$table->increments('id');
$table->string('participant_id')->nullable();
$table->integer('question_id')->nullable();
$table->integer('survey_id')->nullable();
$table->string('session_sid')->nullable();
$table->text('response')->nullable();
$table->enum('type', ['numeric', 'image'])->nullable();
$table->string('mediaSID')->index()->nullable();
$table->string('messageSID')->index()->nullable();
$table->string('mediaURL')->index()->nullable();
$table->binary('media')->nullable();
$table->string('filename')->index()->nullable();
$table->string('MIMEType')->nullable();
$table->timestamps();
});
Schema::table('survey_responses', function($table) {
$table->foreign('participant_id')->references('user_id')->on('participants');
$table->foreign('question_id')->references('id')->on('survey_questions');
$table->foreign('survey_id')->references('survey_id')->on('survey_questions');
});
}
}