将数据保存到数据库后,我按$message->user = Auth::user()->name
并返回$message
。它返回正确的对象。我只是希望在user
中插入message
。
{
"message": {
"id": 46,
"channel": 1,
"by": 1,
"body": "saddla",
"created_at": "2017-07-25 18:46:31",
"updated_at": "2017-07-25 18:46:31",
"user": "Clyde Santiago"
}
}
但是当将$message
传递给Pusher
时,它只返回原始对象。不包括user
。
以下是debug console
pusher
中的数据
{
"message": {
"id": 46,
"channel": 1,
"by": 1,
"body": "saddla",
"created_at": "2017-07-25 18:46:31",
"updated_at": "2017-07-25 18:46:31"
}
}
这是我的MessageController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Message;
use Illuminate\Support\Facades\Auth;
use App\Events\Chat;
class MessageController extends Controller
{
public function create(Request $request){
$message = new Message;
$message->channel = $request->channel_id;
$message->by = Auth::user()->id;
$message->body = $request->message;
$message->save();
$message->user = Auth::user()->name;
event(new Chat($message));
return $message;
}
}