10 months ago

How to reload a foreach or view?

Posted 10 months ago by elargento23

I have a controller which loads a random question:

public function index () {
    $question = Question::orderByRaw('RAND()')->take(1)->get();

    return view('welcome', compact('question'));

Inside the view, there is a foreach which displays the random question:

                                    <div class="block-content">
                                        @foreach($question as $q)
                                        <h2 class="font-w400">{{$q->question_text}}</h2>@endforeach</div>

So, I want to insert a button on the view to get a new random question without having to reload the page (because in the future I want to add some filters through Radiobuttons and such so send certain parameters to the controller and get random questions based on certain criteria).

Now you know the situation, which is the best approach? Should I create a secondary view to load the foreach as a section and then reload the section only?

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