Для моих 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.
Обновлено: после смены метода с поста на получение я получаю другую ошибку.
$community = Community::whereUserId($post->id);
И это то, что я намерен вернуть:
return response () -> json (['success' => true, 'code' => 200, 'comment_id' => $ community]);
Но когда я обновляю страницуЯ получаю исключение:
error_message "You cannot serialize or unserialize PDO instances"