Laravel 5 - 如何计算连接和分组的总记录数

时间:2016-08-24 06:49:06

标签: php mysql laravel group-by

我需要计算总数。来自数据库的记录来自group by。默认情况下,我不需要计算每一组或哪些组。

我知道的一种方法是在查询中使用 - > get(),但是当我在数据库中有大量记录时它会崩溃。

$registration = new user(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE);

// json response
$response = array("error" => FALSE);

$name = (isset($_POST['name'])) ? $_POST['name'] : FALSE;
$email = (isset($_POST['email'])) ? $_POST['email'] : FALSE;
$password = (isset($_POST['password'])) ? $_POST['password'] : FALSE;

if (isset($name) && isset($email) && isset($password)) {

    //$name = $_GET['name'];
    //$email = $_GET['email'];
    //$password = $_GET['password'];

    // check if user is already existed with the same email
    if ($registration->isUserExisted($email)) {
        // already existed
        $response["error"] = TRUE;
        $response["error_msg"] = "User already existed with " . $email;
        echo json_encode($response);
    } else {
        $user = $registration->storeUser($name, $email, $password);
        if ($user) {
            // user stored successfully
            $response["error"] = FALSE;
            $response["uid"] = $user["unique_id"];
            $response["user"]["name"] = $user["name"];
            $response["user"]["email"] = $user["email"];
            $response["user"]["created_at"] = $user["created_at"];
            $response["user"]["updated_at"] = $user["updated_at"];
            echo json_encode($response);
        } else  {
            // user failed to store
            $response["error"] = TRUE;
            $response["error_msg"] = "Unknown error occurred in registration!";
            echo json_encode($response);
        }
    }

} else {
    $response["error"] = TRUE;
    $response["error_msg"] = "Required parameters (name, email or password) is missing!";
    echo json_encode($response);
}

2 个答案:

答案 0 :(得分:1)

从我的评论中复制:

我假设你要分页$orders

$orders->paginate($itemsPerPage)

为您提供与

完全相同的结果
$orders->skip($startFrom)->take($itemsPerPage)->get().

略有不同,因为Model::get()为您提供了集合实例,而Model::paginate()则返回LengthAwarePaginator。但是你可以迭代两者 请参阅laravel docs上的Pagination

答案 1 :(得分:0)

以下是我使用@Skysplit帮助更新的代码:

        // Get orders
        $orders = $store->orders();

        $orders->join('order_product', 'orders.id', '=', 'order_product.order_id');
        $orders->join('products', 'products.id', '=', 'order_product.product_id');
        $orders->join('customers', 'order_product.order_id', '=', 'customers.order_id');
        $orders->join('addresses', 'customers.id', '=', 'addresses.customer_id');
        $orders->where('products.status', 1);
        $orders->where('orders.is_deleted', '0');

        if ($keyword) {
            $orders->where(function ($query) use ($keyword, $searchKeyword){
                $query->where('products.title', 'LIKE', $searchKeyword)
                      ->orWhere('orders.order_name', 'LIKE', $searchKeyword);

                if (strtolower($keyword) == 'enabled') {
                    $query->orWhere('orders.status', '=', 1);
                }elseif (strtolower($keyword) == 'disabled') {
                    $query->orWhere('orders.status', '=', 0);
                }

                return $query;
            });
        }

        $orders->groupby('orders.id');

        $orders->orderBy($orderBy, $orderDirection);
        $orders = $orders->select([
            'orders.id',
            'orders.order_name',
            'products.title',
            'products.handle',
            'products.id as product_id',
            'orders.status'
        ])->paginate($itemsPerPage);

        // Total orders
        $totalOrders = $orders->total();

希望它会对某人有所帮助。