xtremer360
6 months ago

Morph One With View Specific

Posted 6 months ago by xtremer360

I'm trying to figure out how I can implement a morphOne specific view based on the type of quiz it trying to be shown.

Is there a better way to go about this?

public function show(Request $request, Quiz $quiz)
    {
        $type = $quiz->quizzable;

        switch($type->getMorphClass()) {
            case 'App\Models\Passage':
                $wrappedContent = wordwrap($type->text, 50, "\r\n");
                $explodedLines = explode("\r\n", $wrappedContent);

                $lineNumbers = count($explodedLines);
        }

        $questions = $quiz->questions->load('choices');
        $question = $quiz->questions->first();
        $total = $questions->count();
        $data = ['passage' => $passage, 'total' => $total, 'questions' => $questions];

        if ($request->ajax()) {
            return response()->json($data);
        }

        return view('quizzes.show', compact('questions', 'quiz', 'type', 'lineNumbers'));
    }

Please sign in or create an account to participate in this conversation.