laravelでリアルタイムで更新されるチャット機能を導入したいのですが、
イベントを発火させると下記のエラーがコンソールに表示されます。
POST http://localhost:8888/posts/create 500 (Internal Server Error)
ページを更新するとイベントが発火しテキストの内容が送信されるのですが、
ただ、クリックしただけでは何も起きず非同期での通信もできません。
解決方法がどうしても見つけられないので、
何卒、ご教授いただけたらと思います。
上記、宜しくお願い致します。
app/Events/posted.php
<?php namespace App\Events; use App\Models\Post; use Illuminate\Queue\SerializesModels; use Illuminate\Broadcasting\Channel; use Illuminate\Broadcasting\PrivateChannel; use Illuminate\Broadcasting\PresenceChannel; use Illuminate\Broadcasting\InteractsWithSockets; use Illuminate\Contracts\Broadcasting\ShouldBroadcast; class Posted implements ShouldBroadcast { use SerializesModels; /** * @var Post */ public $post; /** * Posted constructor. * @param Post $post */ public function __construct(Post $post) { $this->post = $post; } /** * @return Channel|Channel[] */ public function broadcastOn() { return new Channel('post'); } }
route/channels.php
<?php Broadcast::channel('post', function (){ return true; });
migration
<?php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreatePostsTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('posts', function (Blueprint $table) { $table->increments('id'); $table->text("text"); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('posts'); } }
route/web.php
<?php Route::get("posts", 'PostController@index')->name('post.index'); Route::post("posts/create", 'PostController@create')->name('post.create');
app/Models/Post.php
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; /** * Class Post * @package App\Models * * @property string $text */ class Post extends Model { /** * The attributes that are mass assignable. * * @var array */ protected $fillable = [ 'text' ]; }
contoller
<?php declare(strict_types=1); namespace App\Http\Controllers; use App\Events\Posted; use App\Models\Post; use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; use Illuminate\View\View; class PostController extends Controller { /** * @return View */ public function index() : View { $posts = Post::all(); return view('channels.index',[ "posts" => $posts ]); } /** * @param Request $request * @return JsonResponse */ public function create(Request $request) : JsonResponse { $post = new Post($request->all()); $post->save(); event(new Posted($post)); return response()->json(['message' => '投稿しました。']); } }
resources/views/channels/index.blade.php
@extends('layouts.app') @section('content') <div class="container"> <div class="row justify-content-center"> <div class="col-md-8"> <div class="card"> <div class="card-header"> <ul id="board"> @foreach($posts as $post) <li>{{ $post->text }}</li> @endforeach </ul> </div> <div class="card-body"> <input type="text" id="text"> <input type="submit" value="送信" id="submit"> </div> </div> </div> </div> </div> @endsection
resources/js/bootstrap.js
import Echo from 'laravel-echo' window.Pusher = require('pusher-js'); window.Echo = new Echo({ broadcaster: 'pusher', key: process.env.MIX_PUSHER_APP_KEY, cluster: process.env.MIX_PUSHER_APP_CLUSTER, encrypted: true });
resources/js/app.js
$(document).ready(function() { $.ajaxSetup({ headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') } }); $("#submit").click(function () { const url = "/posts/create"; $.ajax({ url: url, data: { text: $("#text").val() }, method: "POST" }); return false; }); window.Echo.channel('post') .listen('Posted', (e) => { $("#board").append('<li>' + e.post.text + '</li>'); }); });
//App\Providers\BroadcastServiceProvider::class,
上記のコメントアウトは外し、パスワードも.envに記載致しました。
あなたの回答
tips
プレビュー