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

Dabbash's avatar

How to include another function to view ?

Hi everyone,

I build a simple quiz app, and I prepared the route and the controller

this is web.php:

Route::get('/trivia/{slug}', 'Web\TriviaQuizController@index')->name('trivia-quiz');

This is TriviaQuizController were to view the quiz questions :

class TriviaQuizController extends Controller
{
    public function home(){
        $quizzes = Quiz::where('quiz_type', 'trivia')->latest()->get();
        return view('web.trivia-home', compact('quizzes'));
    }

    public function index($slug)
    {   
        $quiz = Quiz::where('slug', $slug)->first();

        if($quiz){
            $questions = Question::where('quiz_id', $quiz->id)->with('media.children', 'trivia_answers.media.children')->get();
        }else{
            abort(404);
        }
        return view('web.quiz.trivia-quiz', compact('questions', 'quiz'));
    }
}

If I have to include an additional function in the same view that supports to get like "show more quizzes"

How I can do that?

0 likes
2 replies
automica's avatar

@dabbash what would show more quizzes do?

BTW in your controller, to match CRUD actions, your method to display all quizzes should be called

index()

and then the method to display one quiz would be called

show($slug)

tisuchi's avatar

@dabbash just write another query and pass to the view-


public function index($slug)
{   
    $quiz = Quiz::where('slug', $slug)->first();

    if($quiz){
        $questions = Question::where('quiz_id', $quiz->id)->with('media.children', 'trivia_answers.media.children')->get();
    } else{
        abort(404);
    }

    $morequizzes = Quiz::get();	// write the appropriate method on quiz model.

    return view('web.quiz.trivia-quiz', compact('questions', 'quiz', 'morequizzes'));
}

Now in the view file, you can easily iterate $morequizzes variable.

Please or to participate in this conversation.