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

jrdavidson's avatar

Morph One With View Specific

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'));
    }
0 likes
3 replies
martinbean's avatar

@xtremer360 In the past, on the morphed classes themselves I’ve added an interface and a getView() method:

interface QuestionType
{
    public function getView();
}
class TextQuestion extends Model implements QuestionType
{
    public function getView()
    {
        return view('questions.text');
    }
}
class MultipleChoiceQuestion extends Model implements QuestionType
{
    public function getView()
    {
        return view('questions.multiple-choices', ['choices' => $this->choices]);
    }
}

Hope that helps!

jrdavidson's avatar

@martinbean Thanks for your response. Well inside of my quizzes.show I have this. So I'm trying to load a dynamic blade file. Reason being the only difference is the content per type. So is the way I am currently trying to do this with the blade views is wrong?

@extends('layouts.passage')

@section('pageTitle', $quiz->title)

@section('content')

    <div id="passage-container" class="container">
        <div id="passage-row" class="row no-gutters">
            <div class="col-lg-6 passage-col mb-5 mb-lg-0">
                <div class="scroll">
                    <div class="passage p-5">
                        <h1 class="h5 text-center mb-5">
                            {{$quiz->title}}
                        </h1>
                        <div class="wrap">
                            @include('quizzes.types'.$type)
                        </div><!-- wrap -->
                    </div><!-- padding -->
                </div><!-- scroll -->
            </div>
            <div class="col-lg-6 passage-col">
                <div class="scroll">
                    <div class="p-5">
                        <quiz-questions id="{{$quiz->id}}"></quiz-questions>
                    </div><!-- padding -->
                </div><!-- scroll -->
            </div>
        </div>
    </div>


@endsection

passage.blade.php

@if( $quiz->id !== 1 )
    <div class="line-numbers">
        @for( $i=1; $i<$lineNumbers; $i++ )
            <span class="{{ ( $i === 4 ) ? 'line' : ''  }}">{{ ( $i === 4 ) ? 'Line' : $i  }}</span>
        @endfor
    </div>
@endif
<div class="passage-text">
    {!!$passage->text!!}
</div><!-- passage text -->
jrdavidson's avatar

Is there something I should do with the code that is specific to a passage view?

$wrappedContent = wordwrap($type->text, 50, "\r\n");
$explodedLines = explode("\r\n", $wrappedContent);

$lineNumbers = count($explodedLines);

Please or to participate in this conversation.