FollowerController.php 1.1 KB

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