如何将对象子对象设置为父对象?

时间:2019-06-19 05:50:55

标签: javascript jquery laravel pusher

我正在与Laravel和Pusher进行评论应用程序。系统已经准备就绪,但是我的对象变成了两个嵌套,所以在拉动屏幕时出现错误:

未定义的[object Object]

问题出在哪里?

我不知道如何使用对象, 但我尝试过这样的事情: Return $comment->comment; 我没有结果。

这是我的事件文件

class Comment implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;

public $comment;

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

}


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

    return array(

        'comment' => $this->comment
    );
}

public function broadcastAs(){
    return 'comment_new';
 }
}

这是我的刀片脚本

   function displayComment(data) {
        let $comment = $('<div>').text(data['comment']).prepend($('<small>').html(data['name'] + "<br>"));
        $('#comments').prepend($comment);
    }

    function addComment(event) {
        function showAlert(message) {
            let $alert = $('#alert');
            $alert.text(message).show();
            setTimeout(() => $alert.hide(), 4000);
        }

        event.preventDefault();
        $('#addCommentBtn').attr('disabled', 'disabled');
        let data = {
            comment: $('#text').val(),
            name: $('#name').val(),
            email: $('#email').val(),
        };
        fetch('/comments', {
            body: JSON.stringify(data),
            credentials: 'same-origin',
            headers: {
                'content-type': 'application/json',
                'x-csrf-token': $('meta[name="csrf- 
          token"]').attr('content'),
                'x-socket-id': window.socketId
            },
            method: 'POST',
            mode: 'cors',
        }).then(response => {
            $('#addCommentBtn').removeAttr('disabled');
            if (response.ok) {
                displayComment(data);
                showAlert('Comment posted!');
            } else {
                showAlert('Your comment was not approved for posting. 
                Please be nicer :)');
            }
        })
    }
</script>
<script src="https://js.pusher.com/4.2/pusher.min.js"></script>
<script>
    var socket = new Pusher('xxxxxxxxxxxxxxxxxxxxx', {
        cluster: 'ap2',
        forceTLS: true
    });
    // set the socket ID when we connect
    socket.connection.bind('connected', function() {
        window.socketId = socket.connection.socket_id;
    });
    socket.subscribe('comments')
        .bind('comment_new',displayComment);
</script>

还有我的控制器

  public function addComment()
  {
    $data = request()->post();

    $comment = Comments::create([
        'name' => $data['name'],
        'parent' => '0',
        'email' => $data['email'],
        'comment' => $data['comment']
    ]);

    event(new Comment($comment));

    Return $comment;


  }

0 个答案:

没有答案