Search code examples
phplaravelpusherpusher-js

Laravel Pusher return view


I using a pusher and I won't return the view when I call the event. I Have a modal with logic and I need to display it when event starting I can't found in google answer about it maybe here can help me.

If need more details I can show all methods thanks.

Event Code

<?php

namespace App\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class TriviaStatus implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets;

    public $question;
    public $answer_options;


    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct($question, $answer_options)
    {
        $this->question  = $question;
        $this->answer_options  = $answer_options;
    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return Channel|array
     */
    public function broadcastOn()
    {
        return ['trivia-status'];
    }

}

Call action

public function SendQuestion(Request $request, $id)
    {

        $this->option->updateLeaderBoardshowAllResult(0);

        //Update All Question;
        $this->question->updateAll($request);

       
        //Update Question Byd ID
        $this->question->update($request, $id);


        event(new TriviaStatus($this->QuestionSentTime()['question'],$this->QuestionSentTime()['answer_options']));

        exit;


        return redirect('/admin/event');
    }

I want return this:

    return view('partials.question_modal', compact('question', 'answer_options'));

Solution

  • My solution
    Event code:

    public function broadcastWith()
        {
            $response = response()
                ->view('partials.question_modal', ['question' => $this->question, 'answer_options' => $this->answer_options], 200)
                ->header('Content-Type', 'application/json');
    
            return [$response->getContent()];
          
        }