現在Laravelでチャットもどきを作っています。
参考URL
上記のサイトを参考に作成していますが、私の場合出力した時の時間がUTC扱いになってしまいます。
Laravelの方とmySQLの方が日本時間設定に(メッセージを保存するときも日本時間で保存できている)なっているはずなのです。(つもり)
しかし、下の画像の様に出力時には下記の画像の様にUTC扱いで困っています。
DBと同じ時間で出力をしたいのですが、方法はありますか?
よろしくお願いいたします。
問題のchat.blade.php
php
1<html> 2<body> 3 <div id="chat"> 4 <textarea v-model="message"></textarea> 5 <br> 6 <button type="button" @click="send">送信</button> 7 8 <div v-for="m in messages"> 9 <!-- 登録された日時 --> 10 <span v-text="m.created_at"></span>: 11 12 <!-- id --> 13 <span v-text="m.user_id"></span>: 14 15 <!-- project_id --> 16 <span v-text="m.project_id"></span>: 17 18 <!-- メッセージ内容 --> 19 <span v-text="m.body"></span> 20 </div> 21 22 </div> 23 <script src="{{ asset('/js/app.js') }}"></script> 24 25 <script> 26 27 new Vue({ 28 el: '#chat', 29 data: { 30 message: '', 31 messages: [], 32 user_id:'', 33 project_id:'', 34 }, 35 methods: { 36 getMessages() { 37 const url = 'パス' 38 axios.get(url) 39 .then((response) => { 40 this.messages = response.data; 41 alert(response.data); 42 }); 43 44 }, 45 send() { 46 47 // const url = action('Ajax\ChatController@create'); 48 const url = 'パス' 49 const params = { message: this.message,user_id:this.user_id,project_id:this.project_id }; 50 axios.post(url, params) 51 .then((response) => { 52 53 this.message = ''; 54 55 }); 56 } 57 }, 58 mounted() { 59 this.getMessages(); 60 Echo.channel('chat') 61 .listen('MessageCreated', (e) => { 62 this.getMessages(); 63 }); 64 } 65 }); 66 67 </script> 68</body> 69</html> 70
コントローラー
php
1<?php 2 3namespace App\Http\Controllers\Ajax; 4 5use App\Events\MessageCreated; 6use App\Http\Controllers\Controller; 7use Illuminate\Http\Request; 8 9class ChatController extends Controller 10{ 11 public function index() { 12 13 return \App\Message::orderBy('created_at', 'desc')->get(); 14 } 15 16 public function create(Request $request) { 17 18 $message = \App\Message::create([ 19 'body' => $request->message, 20 'user_id' => 12345, 21 'project_id' => 99999 22 ]); 23 event(new MessageCreated($message)); 24 25 } 26} 27
ほぼ上記の参考URLに書かれていることを書いているだけですが、よろしくお願いいたします。
**9/2 **
- SQLの時間を記録しているSQLの'created_at'の型をtimestampからdatetimeに変えてやってみましたが変化がありませんでした。
- \Log::debug()でsqlから引っ張ってきた配列を確認したところ
php
1[{"id":1,"project_id":"999999","body":"1\u756a\u76ee\u306e\u30e1\u30c3\u30bb\u30fc\u30b8","created_at":"2020-09-02T06:26:36.000000Z","updated_at":"2020-09-02T06:26:36.000000Z"},省略]
の様になっており、すでに時間がUTC扱い?の様に見受けられました。
つまり、Vueが何かやっているわけではない様です。(migrateしているので画像と時間が違います)
- LaravelのタイムゾーンをUTCに戻したところ、当然ながらUTC時間でSQLに保存されました。
また、それに伴い、SQLから引き出すと時間は登録された時間からさらに9時間遡って表示されました。。。
現在分かったのは、LaravelがSQLから取り出す瞬間に時間が変換されている様です。
cakePHP3を使用していた時はこんなことは体験しなかったので、mysqlが悪いとは思えません。
現在Laravelが何かしらやっていると睨んでいます。
回答1件
あなたの回答
tips
プレビュー