Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mtlaney's avatar

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>
0 likes
5 replies
mtlaney's avatar

Edit: still unable to get them to step thru, any help would be HIGHLY appreciated

Cronix's avatar

Looks like you need to create an array of charts

foreach ($survey->questions as $key=>$question){
          $chart = Charts::database($answer->where('question_id', '=', $survey->questions->pluck('id'))->get(), 'bar', 'material')
...

You're overwriting $chart on each loop, so $chart will only be whatever the last loop was

Maybe something like $chart[$key] = Charts::... and then access that $chart[$key]->render() in the view in your loop.

mtlaney's avatar

I think we're on the right track, but still no luck.

Cannot use object of type ConsoleTVs\Charts\Facades\Charts as array

Cronix's avatar

Yeah, sorry, I'm not sure on that. I haven't used that package. I just noticed the loop where you're overwriting the variable on each iteration.

mtlaney's avatar

No worries, I appreciate the help. Trying to get this solved, last part of this project before I can wash my hands of it!

Please or to participate in this conversation.