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);
}
}