首页 > 解决方案 > 在 Laravel 中使用推送器进行广播时不显示内容

问题描述

我正在开发一个使用 Laravel 7 和 Pusher 作为广播驱动程序的实时聊天应用程序。我正面临一个奇怪的问题,因为它广播了一个空对象,但在我的控制台中,我收到了正确的对象/评论。

在此处输入图像描述

indexComponent.vue:

<template>
    <div>
        <h1>All Comments</h1>
        <div style="margin-bottom:20px;">
            <textarea class="form-control" rows="3" name="body" placeholder="write a comment" v-model="commentBox" ></textarea>
            <button class="btn btn-success" style="margin-top:10px" v-on:click="createComment()" >Save Comment</button>
        </div>

        <div v-for='comment in this.listOfComments' v-bind:key="comment.id" class='card card-body mb-2'>
            <p>{{comment.body}}</p>
        </div>
    </div>
</template>

<script>
    export default {
        props: ['comments'],
        data() {
            return {
                commentBox: '',
                listOfComments: this.comments,
            }
        },
        created: function() {
            this.listen();
        },
        methods: {
            createComment: function() {
                axios.post('/api/comment', {
                    body: this.commentBox,
                    title: 'from axios',
                    user_id: '1'
                })
                    .then(response => {
                        this.listOfComments.unshift(response.data)
                        this.commentBox = '';
                    })
                    .catch(error => {
                        console.log(error.response);
                    });
            },
            listen: function() {
                Echo.channel('commentsChannel')
                    .listen('NewComment', (cmn) => {
                        console.log(cmn);
                        this.listOfComments.unshift(cmn);
                        console.log(this.comments[0]);
                    })
            }
        },
    }
</script>

NewComment.php(事件)

<?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\Contracts\Broadcasting\ShouldBroadcastNow;  //for no queueing 
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

use App\Comment;

class NewComment implements ShouldBroadcastNow
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public $comment;

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

    /**
     * Get the channels the event should broadcast on.
     *
     * @return \Illuminate\Broadcasting\Channel|array
     */
    public function broadcastOn()
    {
        return new Channel('commentsChannel');
    }
}

评论控制器.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Comment;
use Auth;
use App\Events\NewComment;


class CommentsController extends Controller {

    public function index() {
        $comments = Comment::orderBy('created_at','desc')->get();
        return view('comments/index')->with('comments',$comments);
    }

    public function store(Request $request) {
        $comment = new Comment;
        $comment->body  = $request->body;
        $comment->title = $request->title;
        $comment->user_id = $request->user_id;

        if($comment->save()){
            // event(new NewComment($comment));
            broadcast(new NewComment($comment))->toOthers();
            return $comment->toJson();
        }
    }

}

请注意,当我提交了一个新的评论事件正在发生时,一个新的部门已经创建,但它是空的。任何人都可以帮助解决这个问题?!先感谢您

标签: laravelvue.jspusher-js

解决方案


推荐阅读