For a start you can do the following:
Investigation::whereYear('created_at', 2022)
->where('status', 'Stalled')
->where('type', 'Enquiry')
->count()
....which will return integers and you don't need to count collections returned
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hi Guys, I have a dashboard that is querying the same model several times in the same call with minor differences. Just looking to find out how I can clean this up a bit more and use less code/kill the repetition.
public function index()
{
$user = Auth::user();
$posts = Post::where('category', 'Post')->orderBy('created_at', 'Desc')->simplePaginate(5);
$investigations = Investigation::orderBy('created_at', 'Desc')->get();
$received_investigations = count(Investigation::whereYear('created_at', 2022)->where('status', 'Open')->where('type', 'Investigation')->get());
$received_reviews = count(Investigation::whereYear('created_at', 2022)->where('status', 'Open')->where('type', 'Review')->get());
$received_enquiries = count(Investigation::whereYear('created_at', 2022)->where('status', 'Open')->where('type', 'Enquiry')->get());
$open_investigations = count(Investigation::whereYear('created_at', 2022)->where('status', 'Open')->where('type', 'Investigation')->get());
$open_reviews = count(Investigation::whereYear('created_at', 2022)->where('status', 'Open')->where('type', 'Review')->get());
$open_enquiries = count(Investigation::whereYear('created_at', 2022)->where('status', 'Open')->where('type', 'Enquiry')->get());
$closed_investigations = count(Investigation::whereYear('created_at', 2022)->where('status', 'Closed')->where('type', 'Investigation')->get());
$closed_reviews = count(Investigation::whereYear('created_at', 2022)->where('status', 'Closed')->where('type', 'Review')->get());
$closed_enquiries = count(Investigation::whereYear('created_at', 2022)->where('status', 'Closed')->where('type', 'Enquiry')->get());
$stalled_investigations = count(Investigation::whereYear('created_at', 2022)->where('status', 'Stalled')->where('type', 'Investigation')->get());
$stalled_reviews = count(Investigation::whereYear('created_at', 2022)->where('status', 'Stalled')->where('type', 'Review')->get());
$stalled_enquiries = count(Investigation::whereYear('created_at', 2022)->where('status', 'Stalled')->where('type', 'Enquiry')->get());
$data = ([
'page_name' => 'Dashboard',
'user' => $user,
'posts' => $posts,
'investigations' => $investigations,
'received_investigations' => $received_investigations,
'received_reviews' => $received_reviews,
'received_enquiries' => $received_enquiries,
'open_investigations' => $open_investigations,
'open_reviews' => $open_reviews,
'open_enquiries' => $open_enquiries,
'closed_investigations' => $closed_investigations,
'closed_reviews' => $closed_reviews,
'closed_enquiries' => $closed_enquiries,
'stalled_investigations' => $stalled_investigations,
'stalled_reviews' => $stalled_reviews,
'stalled_enquiries' => $stalled_enquiries,
]);
return view('dashboard')->with($data);
}
Please or to participate in this conversation.