DiscoverController.php 1.1 KB

12345678910111213141516171819202122232425262728293031
  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. $following = Follower::whereProfileId(Auth::user()->profile->id)->pluck('following_id');
  15. $people = Profile::inRandomOrder()->where('id', '!=', Auth::user()->profile->id)->whereNotIn('id', $following)->take(3)->get();
  16. $posts = Status::whereHas('media')->where('profile_id', '!=', Auth::user()->profile->id)->whereNotIn('profile_id', $following)->orderBy('created_at', 'desc')->take('21')->get();
  17. return view('discover.home', compact('people', 'posts'));
  18. }
  19. public function showTags(Request $request, $hashtag)
  20. {
  21. $tag = Hashtag::whereSlug($hashtag)->firstOrFail();
  22. $posts = $tag->posts()->has('media')->orderBy('id','desc')->paginate(12);
  23. $count = $tag->posts()->has('media')->orderBy('id','desc')->count();
  24. return view('discover.tags.show', compact('tag', 'posts', 'count'));
  25. }
  26. }