HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers10/zelfverkopen.komma.pro/app/KommaApp/Questions/QuestionComposer.php
<?php


namespace App\KommaApp\Questions;


use App\KommaApp\Questions\Models\Question;
use Illuminate\View\View;

class QuestionComposer
{
    /**
     * Bind data to the view.
     *
     * @param  View $view
     * @return void
     */
    public function compose(View $view)
    {
        $questions = Question::where('active', 1)
            ->where('lft', '!=', 1)
            ->orderBy('lft')
            ->with('translation')
            ->with('question_categories')
            ->with('question_categories.translation')
            ->get();


        $questionCategories = [];

        // Loop through questions
        foreach ($questions as $question){

            // Loop through each category of the question
            foreach ($question->question_categories as $question_category){

                // Generate slug
                $categorySlug = $question_category->translation->slug;

                // If category is already defined in the array, only append the question to the questions array
                if(isset($questionCategories[$categorySlug])){
                    $questionCategories[$categorySlug]->questions[] = $question;
                }
                // Else define the new category
                else $questionCategories[$categorySlug] = (object)[ 'category' => $question_category, 'questions' => [$question]];
            }
        }

        // Order keys by alphabet
        ksort($questionCategories);

        $view->with('questions', $questions)
            ->with('categories', $questionCategories);
    }
}