0

I want to display the number of records from several tables at once in one view. I've tried it using eloquent count.

public function index(){
    $order = Order::count();
    $owner = Owner::count();
    $room = Room::count();
    $member = Transaction::where([
        ['status', 'waiting'],
        ['type', 1]
        ])->count();

    $highlight = Transaction::where([
        ['status', 'waiting'],
        ['type', 2]
        ])->count();


    return view('admin.index', [
        'order' => $order,
        'owner' => $owner,
        'room' => $room,
        'member' => $member,
        'highlight' => $highlight
    ]);
}

Is there a better way?

barkah
  • 75
  • 1
  • 10

1 Answers1

0

You could also use view-composers. But this is not better, just different. I mean you have to query anyway, so why do you think there should be a better way?

Flo Espen
  • 450
  • 4
  • 10
  • Thanks before. I just think there is a better query approach. Suppose a raw query @Flo – barkah Sep 05 '20 at 07:03
  • Imho your approach is fine. You can compare queries like this: `DB::connection()->enableQueryLog(); User::count(); dd(DB::getQueryLog());` Like this you also get the query time and the query that was executed. – Flo Espen Sep 05 '20 at 09:29