File: D:/HostingSpaces/SBogers10/momsecurity.komma.nl/app/Questions/QuestionService.php
<?php
namespace App\Questions;
use App\Base\Service;
use App\QuestionCategories\Models\QuestionCategory;
use Illuminate\Support\Collection;
final class QuestionService extends Service
{
private $questionCategories;
public function __construct()
{
parent::__construct();
// Set options first if needed
if(!isset($this->questionCategories)) $this->setQuestionCategories();
}
public function setQuestionCategories()
{
$this->questionCategories = QuestionCategory::where('active', 1)
->with('translation', 'questions', 'questions.translation')
->has('translation')
->has('questions')
->orderBy('lft')
->get();
}
/**
* @return mixed
*/
public function getQuestionsByCategory()
{
return $this->questionCategories;
}
/**
* Get the question for the asked code named category
*
* @param string $codeName
* @return Collection
*/
public function getQuestionsForCategory(string $codeName): Collection
{
$category = $this->questionCategories->where('code_name', $codeName)->first();
if(!isset($category)) return collect();
return $category->questions;
}
}