You can achieve some improvements using appropriate indexes on the database table.
Jun 30, 2022
2
Level 5
Advice on making my code more efficient
My app is... let's call it a music repository. I am at the stage now where some of my clients are storing hundreds of tracks. I never expected this when I started this thing as a hobby / weekend fuckabout project - however here we are.
Some of my power users are getting timeout errors now when they view their tracks page as the system is away getting hundreds of tracks plus some data on them like play counts etc.
For the sake of argument let's say that pagination is not a viable option. Is there a more efficient way for me get and display these tracks?
My current code is below:
public function index()
{
$user = Auth::user();
$userid = $user->id;
$trackcount = Track::countTracks($userid);
$tracks = Track::where([
['user_id', $userid],
['group_token', null],
['status', 1]
])->orderBy('created_at', 'DESC')->get();
return view('tracks', compact('tracks', 'trackcount'));
}
Level 104
2 likes
Please or to participate in this conversation.