FollowerController.php 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Auth;
  4. use App\{Follower, Profile};
  5. use Illuminate\Http\Request;
  6. use App\Jobs\FollowPipeline\FollowPipeline;
  7. class FollowerController extends Controller
  8. {
  9. public function __construct()
  10. {
  11. $this->middleware('auth');
  12. }
  13. public function store(Request $request)
  14. {
  15. $this->validate($request, [
  16. 'item' => 'required|integer',
  17. ]);
  18. $user = Auth::user()->profile;
  19. $target = Profile::where('id', '!=', $user->id)->findOrFail($request->input('item'));
  20. $isFollowing = Follower::whereProfileId($user->id)->whereFollowingId($target->id)->count();
  21. if($isFollowing == 0) {
  22. $follower = new Follower;
  23. $follower->profile_id = $user->id;
  24. $follower->following_id = $target->id;
  25. $follower->save();
  26. FollowPipeline::dispatch($follower);
  27. } else {
  28. $follower = Follower::whereProfileId($user->id)->whereFollowingId($target->id)->firstOrFail();
  29. $follower->delete();
  30. }
  31. return redirect()->back();
  32. }
  33. }