can you show some idea so that we can have an idea what went wrong
After two second the page reload through the ajax from the controller in laravel
After two second the page reload through the ajax from the controller in laravel. Pls help me.
i have implement the chat functionality in project but when i send message to another user than message will not show but when we reload the page the message will show correct. so i want to slove this problem pls help me.
This is my controller code...
This is my ajax page..
$(document).ready(function () {
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$('#talkSendMessage').on('submit', function(e) {
e.preventDefault();
var url, request, tag, data;
tag = $(this);
url = __baseUrl + '/client/ajax/message/send';
data = tag.serialize();
request = $.ajax({
method: "post",
url: url,
data: data
});
request.done(function (response) {
if (response.status == 'success') {
$('#talkMessages').append(response.html);
tag[0].reset();
}
});
});
$('body').on('click', '.talkDeleteMessage', function (e) {
e.preventDefault();
var tag, url, id, request;
tag = $(this);
id = tag.data('message-id');
url = __baseUrl + '/client/ajax/message/delete/' + id;
if(!confirm('Do you want to delete this message?')) {
return false;
}
request = $.ajax({
method: "post",
url: url,
data: {"_method": "DELETE"}
});
request.done(function(response) {
if (response.status == 'success') {
$('#message-' + id).hide(500, function () {
$(this).remove();
});
}
});
})
});
as you can see your controller is not displaying please add ``` before writing your code and also show the view/blade code
'<?php
namespace App\Http\Controllers\Client; use App\User; use Illuminate\Http\Request; use Nahid\Talk\Facades\Talk; use App\Http\Controllers\Controller; use Auth; use View;
class MessageController extends Controller {
protected $layout = 'client.layout.master';
protected $title = '';
protected $content = '';
public function setlayout(){
return view($this->layout,['title'=>$this->title,'content'=>$this->content]);
}
protected $authUser;
public function __construct()
{
$this->middleware('auth');
Talk::setAuthUserId(Auth::user()->id);
View::composer('client.partials.peoplelist', function($view) {
$threads = Talk::threads();
$view->with(compact('threads'));
});
}
public function chatHistory($id)
{
$conversations = Talk::getMessagesByUserId($id);
$user = '';
$messages = [];
if(!$conversations) {
$user = User::find($id);
} else {
$user = $conversations->withUser;
$messages = $conversations->messages;
print_r($messages);
die();
} return view('client.messages.conversations', compact('messages', 'user')); }
public function ajaxSendMessage(Request $request)
{
if ($request->ajax()) {
$rules = [
'message-data'=>'required',
'_id'=>'required'
];
$this->validate($request, $rules);
$body = $request->input('message-data');
$userId = $request->input('_id');
if ($message = Talk::sendMessageByUserId($userId, $body)) {
$html = view('client.ajax.newMessageHtml', compact('message'))->render();
return response()->json(['status'=>'success', 'html'=>$html], 200);
}
}
}
public function ajaxDeleteMessage(Request $request, $id)
{
if ($request->ajax()) {
if(Talk::deleteMessage($id)) {
return response()->json(['status'=>'success'], 200);
}
return response()->json(['status'=>'errors', 'msg'=>'something went wrong'], 401);
}
}
public function tests()
{
dd(Talk::channel());
}
} '
blade file
'@extends('client.layout.chat')
@section('content')
<div class="chat-history">
<ul id="talkMessages">
@foreach($messages as $message)
@if($message->sender->id == auth()->user()->id)
<li class="clearfix" id="message-{{$message->id}}">
<div class="message-data align-right">
<span class="message-data-time" >{{$message->humans_time}} ago</span>
<span class="message-data-name" >{{$message->sender->name}}</span>
<a href="#" class="talkDeleteMessage" data-message-id="{{$message->id}}" title="Delete Message"><i class="fa fa-close"></i></a>
</div>
<div class="message other-message float-right">
{{$message->message}}
</div>
</li>
@else
<li id="message-{{$message->id}}">
<div class="message-data">
<span class="message-data-name"> <a href="#" class="talkDeleteMessage" data-message-id="{{$message->id}}" title="Delete Messag"><i class="fa fa-close" style="margin-right: 3px;"></i></a>{{$message->sender->name}}</span>
<span class="message-data-time">{{$message->humans_time}} ago</span>
</div>
<div class="message my-message" >
{{$message->message}}
</div>
</li>
@endif
@endforeach
</ul>
</div> <!-- end chat-history -->
Please help me for this solution.
can you explain what exactly you want and what problem your are getting and before placing your code please add ```(three `) so that it could be readable
Using ordinary ajax request for something real time like a chat is definitely the wrong way to go.
Take a look at the Broadcasting section and Laravel Echo in the Laravel documentation for a much better way to handle real-time events.
https://laravel.com/docs/5.4/broadcasting#installing-laravel-echo
Some relevant videos from this site.
https://laracasts.com/lessons/introducing-laravel-echo
https://laracasts.com/series/real-time-laravel-with-socket-io
https://laracasts.com/lessons/pusher-awesomeness
https://laracasts.com/lessons/broadcasting-events-in-laravel-5-1
i have implement the chat functionality in project but when i send message to another user than message will not show but when we reload the page the message will show correct. so i want to slove this problem pls help me.
I download the from that link https://github.com/nahid/talk/.And implement into the our project. But all the functionality working properly but when i send message to another user than message will not show but when we reload the page the message will show correct. so i want to slove this problem pls help me.
Please or to participate in this conversation.