DiscoverController.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Follower;
  4. use App\Hashtag;
  5. use App\Profile;
  6. use App\Status;
  7. use Auth;
  8. use Illuminate\Http\Request;
  9. class DiscoverController extends Controller
  10. {
  11. public function __construct()
  12. {
  13. $this->middleware('auth');
  14. }
  15. public function home()
  16. {
  17. $pid = Auth::user()->profile->id;
  18. $following = Follower::whereProfileId($pid)
  19. ->pluck('following_id');
  20. $people = Profile::inRandomOrder()
  21. ->where('id', '!=', $pid)
  22. ->whereNotIn('id', $following)
  23. ->take(3)
  24. ->get();
  25. $posts = Status::whereHas('media')
  26. ->where('profile_id', '!=', $pid)
  27. ->whereNotIn('profile_id', $following)
  28. ->orderBy('created_at', 'desc')
  29. ->simplePaginate(21);
  30. return view('discover.home', compact('people', 'posts'));
  31. }
  32. public function showTags(Request $request, $hashtag)
  33. {
  34. $this->validate($request, [
  35. 'page' => 'nullable|integer|min:1|max:10',
  36. ]);
  37. $tag = Hashtag::with('posts')
  38. ->withCount('posts')
  39. ->whereSlug($hashtag)
  40. ->firstOrFail();
  41. $posts = $tag->posts()
  42. ->whereIsNsfw(false)
  43. ->whereVisibility('public')
  44. ->has('media')
  45. ->orderBy('id', 'desc')
  46. ->simplePaginate(12);
  47. return view('discover.tags.show', compact('tag', 'posts'));
  48. }
  49. }