对于我的api,我想在社区的帖子中发布评论时通知社区的所有者,我的代码如下所示,并且函数如下所示:
public function communityPostComment(Request $request, $post_id) {
$user = $this->getAuthorizedUser($request->header('X-User-Auth-Token'));
$user = User::find(6999);
if (!$user) {
return response()->json(['error' => true, 'code' => 401, 'message' => INVALID_AUTH_TOKEN], 401);
}
//$text = $request->input('comment_text');
$text = "This is my latest";
if (is_null($text) || $text == '') {
return response()->json(['error' => true, 'code' => 400, 'message' => 'No comment text submitted']);
}
try {
if(0 && \App::environment('production-trigger')) {
$insert = \DB::connection('trigger')->table('tbl_Community_Post_Comments')->insertGetId([
'User_ID' => $user->id,
'Post_ID' => $post_id,
'Comment_Text' => $text,
'Created_Date_Time' => Carbon::now()->format('Y-m-d H:i:s'),
'Active' => 1,
]);
$post = \DB::table('community_custom_posts')->wherePostId($post_id)->first();
$community = Community::whereUserId($post->community_id);
event(new SendPostCommentNotification($community, $user));
} else {
$insert = CommunityPostComment::insertGetId([
'post_id' => $post_id,
'user_id' => $user->id,
'comment_text' => $text,
'active' => 1,
'created_at' => Carbon::now()->format('Y-m-d H:i:s'),
]);
$post = \DB::table('community_custom_posts')->wherePostId($post_id)->first();
$community = Community::whereUserId($post->community_id)->get();
event(new SendPostCommentNotification($community, $user));
}
return response()->json(['success' => true, 'code' => 200, 'comment_id' => $insert]);
} catch (\PDOException $e) {
return response()->json(['error' => true, 'code' => 400, 'message' => 'Unsuccessful comment attempt', 'error_message' => $e->getMessage()]);
}
}
这是我的路线的样子:
Route::post('post/comment/{post_id}', 'GolfPlayed\APIController@communityPostComment')->where('post_id', '[0-9A-Za-z\-]+')->name('api.communityPostComment');
当我键入此内容时:
http://productionapi.local/post/comment/613
由于某种原因,我得到了一个方法notallowedexception
我也在屏幕上看到了这个
public function get($method = null)
{
return is_null($method) ? $this->getRoutes() : Arr::get($this->routes, $method, []);
}
/**
* Determine if the route collection contains a given named route.
已更新: 从post更改为get的方法后,出现另一个错误。
$community = Community::whereUserId($post->id);
这就是我打算返回的内容:
return response()-> json(['success'=> true,'code'=> 200,'comment_id'=> $ community]);
但是当我刷新页面时,我收到一个异常:
error_message "You cannot serialize or unserialize PDO instances"
答案 0 :(得分:0)
Http方法是POST
您可以使用REST Client(如Postman)进行发布请求
https://www.getpostman.com/
已更新:
您忘记致电->get()
或->first()
$community = Community::whereUserId($post->community_id)->get();