我想制作Pusher实时laravel聊天应用程序,但是我被困在广播并将聊天内容保存到数据库中。
我的用户模型上有messages()
方法,但是Laravel抛出异常
调用未定义的方法App \ User :: messages()
这是我的用户模型
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
public function messages()
{
return $this->hasMany('App\Message');
}
}
这是我的控制器
use App\Message;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class ChatsController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function index()
{
return view('chat');
}
public function fetchMessages()
{
return Message::with('user')->get();
}
public function sendMessage(Request $request)
{
$user = Auth::user();
$message = $user->messages()->create([
'message' => $request->input('message')
]);
return ['status' => 'Message Sent!', 'user' => $user];
}
}
这是返回的响应
"message": "Call to undefined method App\\User::messages()",
"exception": "BadMethodCallException",
"file": "C:\\nginx\\html\\eyducation\\vendor\\laravel\\framework\\src\\Illuminate\\Support\\Traits\\ForwardsCalls.php",
"line": 50,
希望大家能帮助我