记录多张表的最佳计数方法-Laravel

问题描述

我想在一个视图中一次显示多个表中的记录数。 我已经尝试过用雄辩的计数了。

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

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


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

有更好的方法吗?

解决方法

您也可以使用view-composers。但这并没有更好,只是有所不同。我的意思是无论如何都要查询,为什么您认为应该有更好的方法呢?