Edit: still unable to get them to step thru, any help would be HIGHLY appreciated
Sep 6, 2017
5
Level 1
How to display charts properly?
Basically I have 3 tables, Survey, Questions, and Answers. I'm trying to decipher a way to get ConsoleTV/Charts to display properly for each question within the survey controller. I can get it to properly display the first question for each survey, or the last one, but it will just repeat them throughout. I'm trying to get them to display for each question. Any help would be greatly appreciated.
Also, this is my first laravel project, and I'm sorry if this is an obvious answer I'm missing.
Survey Model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Survey extends Model
{
protected $fillable = ['title', 'description', 'user_id'];
protected $dates = ['deleted_at'];
protected $table = 'surveys';
public function questions(){
return $this->hasMany(Question::class);
}
public function user(){
return $this->belongsTo(User::class);
}
public function answers(){
return $this->hasMany(Answer::class);
}
}
Controller section in question
public function view_survey_answers(Survey $survey, Charts $chart, Answer $answer, Question $question)
{
$survey = $survey->load('user.questions.answers');
$id = $survey->id;
$stuff = $survey->questions->pluck('title');
$ball = $survey->questions->pluck('id');
foreach ($survey->questions as $key=>$question){
$chart = Charts::database($answer->where('question_id', '=', $survey->questions->pluck('id'))->get(), 'bar', 'material')
->title("")
->colors(['#F5A623', '#A9D8D3'])
->elementLabel("Answers")
->dimensions(0, 250)
->groupBy('answer');
}
return view('answer.view', compact('survey', 'chart', 'answer', 'question', 'chart2', 'ball', 'stuff'));
}
View
<div class="container-main col-md-10 col-md-offset-1">
<h2>{{ $survey->title }}</h2>
@forelse ($survey->questions as $key=>$question)
<div class="col-md-6 question-guide" style="float: left;">
<!-- ____________________IF QUESTION IS TEXT OR TEXT AREA DISPLAYING ANSWERS____________________-->
@if($question->question_type === 'text' || $question->question_type === 'textarea')
<h4 class="question">Question {{$key+1}} {{ $question->title }} </h4>
{!! $chart->render() !!}
<h5 class="answer-other">Answers:</h5>
@foreach ($question->answers as $answer)
<p class="answer"><strong>{{ $answer->answer }}</strong></p>
@endforeach
<!-- ____________________IF QUESTION IS CHECKBOX OR RADIO AREA DISPLAYING ANSWERS____________________-->
@elseif($question->question_type === 'checkbox' || $question->question_type === 'radio')
<h4 class="question">Question {{$key+1}} {{ $question->title }} </h4>
<p class="option">Options:</p>
@foreach ($question->option_name as $option)
<p class="option">| {{ $option }} </p>
@endforeach
<h5 class="answer-other">Answers:</h5>
@foreach ($question->answers as $answer)
<p class="answer-other"><strong>{{ $answer->answer }}</strong></p>
@endforeach
@endif
</div>
@empty
@endforelse
</div>
Please or to participate in this conversation.