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

Josadec's avatar

Empty results when print table

I tried to make a report from multiple tables, with livewire but when I tried to print it the table is show nothing, somebody could help me with this case

Or they have a better idea to did this report, please tell me

class CategoriesResults extends Component
{
    public function render()
    {   
        $answers = Answer::join('questions','answers.question_id','=','questions.id')
        ->join('answer_application','answer_application.answer_id','=','answers.id')
        ->join('dimensions','dimensions.id','=','questions.dimension_id')
        ->join('domains','domains.id','=','dimensions.domain_id')
        ->join('categories','categories.id','=','domains.category_id')
        ->select('categories.name AS category_name, 
                  SUM(IF(answers.description = "Siempre",categories.id,0)) AS total_always,
                  SUM(IF(answers.description = "Casi siempre",categories.id,0)) AS total_usually,
                  SUM(IF(answers.description = "Algunas veces",categories.id,0)) AS total_sometimes,
                  SUM(IF(answers.description = "Casi nunca",categories.id,0)) AS total_almostnever,
                  SUM(IF(answers.description = "Nunca",categories.id,0)) AS total_never
        ')
        ->groupBy('categories.name')
        ->orderBy('categories.id')
        ->get()
        ;

        return view('livewire.categoriesresults.categories-results',compact('answers'));
    }
}

Blade View

           <tbody>
              @foreach($answers as $answer)
                    <tr class="text-center">
                          <td>{{ $id++; }}</td>
                          <td>{{ $answer->category_name }}</td>
                          <td>{{ $answer->total_always }}</td>
                          <td>{{ $answer->total_usually }}</td>
                          <td>{{ $answer->total_sometimes }}</td>
                          <td>{{ $answer->total_allmostnever }}</td>
                          <td>{{ $answer->total_never}}</td>
                    </tr>
 
              @endforeach
            </tbody>

Results when running this command dd($answers);

Illuminate\Database\Eloquent\Collection {#1714 ▼
  #items: array:5 [▼
    0 => App\Models\Answer {#1713 ▼
      #connection: "mysql"
      #table: "answers"
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      +preventsLazyLoading: false
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #escapeWhenCastingToString: false
      #attributes: array:1 [▼
        "category_name,                   SUM(IF(answers.description = "Siempre",categories.id,0))" => "Ambiente de trabajo"
      ]
      #original: array:1 [▶]
      #changes: []
      #casts: []
      #classCastCache: []
      #attributeCastCache: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: []
      #touches: []
      +timestamps: true
      #hidden: []
      #visible: []
      #fillable: []
      #guarded: []
    }
    1 => App\Models\Answer {#1712 ▶}
    2 => App\Models\Answer {#1711 ▶}
    3 => App\Models\Answer {#1710 ▶}
    4 => App\Models\Answer {#1709 ▶}
  ]
  #escapeWhenCastingToString: false
}
0 likes
4 replies
tykus's avatar

->select should be ->selectRaw

Josadec's avatar

@tykus Okay, How can make the comparison? could get me an example, please

tykus's avatar
tykus
Best Answer
Level 104

@Josadec I meant in the query:

$answers = Answer::join('questions','answers.question_id','=','questions.id')
        ->join('answer_application','answer_application.answer_id','=','answers.id')
        ->join('dimensions','dimensions.id','=','questions.dimension_id')
        ->join('domains','domains.id','=','dimensions.domain_id')
        ->join('categories','categories.id','=','domains.category_id')
        ->selectRaw('categories.name AS category_name, 
                  SUM(IF(answers.description = "Siempre",categories.id,0)) AS total_always,
                  SUM(IF(answers.description = "Casi siempre",categories.id,0)) AS total_usually,
                  SUM(IF(answers.description = "Algunas veces",categories.id,0)) AS total_sometimes,
                  SUM(IF(answers.description = "Casi nunca",categories.id,0)) AS total_almostnever,
                  SUM(IF(answers.description = "Nunca",categories.id,0)) AS total_never
        ')
        ->groupBy('categories.name')
        ->orderBy('categories.id')
        ->get();

Please or to participate in this conversation.