splendidkeen
8 months ago

HomeController::index (Long loading time) Collection issue

Posted 8 months ago by splendidkeen

I am currently thinking about collection-related performance. At the moment I am structuring collections as described below, but the homepage loads very long. I am handling collections for both, authenticated Users, as well as guest Users.

public function index()
    {   

        if(Auth::check()){

          if (auth()->user()->language_id == 1) {
              $language = 'en';
          } elseif (auth()->user()->language_id == 2) {
              $language = 'de';
          } else {
              $language = 'en';
          }
          Carbon\Carbon::setLocale($language);
        } 


        if(Auth::check()){
          $friends = Friend::where('city', Auth::user()->city)->limit(4)->get(); 
          $statuses1 = Status::where('user_id', Auth::user()->id)->limit(8)->get(); 
        }else{
          $partners = Friend::limit(3)->get(); 
          $statuses2 = Status::where('city', '=', '1')->limit(8)->get(); 
        
        }

        $friends1 = Friend::inRandomOrder()->limit(4)->get(); 
        $friendscity = Friend::inRandomOrder()->where('city', '=', '1')->limit(4)->get(); 
        $friends2 = Friend::limit(6)->get(); 
        $statuses = Status::limit(4)->get(); 

  

        return view('home', compact('friends','statuses1', 'partners', 'statuses2', 'friends1', 'friendscity', 'friends2', 'statuses'));
    }   

Is there a way to increase the efficiency of my collections in terms of logic and performance?

Please sign in or create an account to participate in this conversation.