CommentController.php 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use Auth;
  5. use App\{Comment, Profile, Status};
  6. use Vinkla\Hashids\Facades\Hashids;
  7. class CommentController extends Controller
  8. {
  9. public function store(Request $request)
  10. {
  11. if(Auth::check() === false) { abort(403); }
  12. $this->validate($request, [
  13. 'item' => 'required|alpha_num',
  14. 'comment' => 'required|string|max:500'
  15. ]);
  16. try {
  17. $statusId = Hashids::decode($request->item)[0];
  18. } catch (Exception $e) {
  19. abort(500);
  20. }
  21. $user = Auth::user();
  22. $profile = $user->profile;
  23. $status = Status::findOrFail($statusId);
  24. $comment = new Comment;
  25. $comment->profile_id = $profile->id;
  26. $comment->user_id = $user->id;
  27. $comment->status_id = $status->id;
  28. $comment->comment = e($request->comment);
  29. $comment->rendered = e($request->comment);
  30. $comment->is_remote = false;
  31. $comment->entities = null;
  32. $comment->save();
  33. return redirect($status->url());
  34. }
  35. }