DiscoverController.php 1.4 KB

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