Laravel存储库模式查询构建

时间:2018-03-21 16:05:05

标签: php laravel laravel-5 eloquent

我已经建立了从数据库中获取记录的存储库。一切正常,但如果会话中有变量,我想添加orderBy方法:

到目前为止,我有这个:

<?php

namespace App\Repositories;

use App\Models\User;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Session;

class UserRepository
{
    protected $user;
    private   $columns;

    public function __construct(User $user)
    {
        $this->user    = $user;
        $this->columns = ['id', 'email', 'pwd', 'name', 'addr', 'addr_detail', 'addr_no', 'city', 'zip', 'state', 'phone', 'company_number', 'vat_id', 'tax_id', 'unique_string', 'active', 'banned', 'admin'];
    }

    public function getAll()
    {
        if (Session::has('sort')) 
        {
            $sort = Session::get('sort');
            if (isset($sort["users"]) && !empty($sort["users"]))
                $this->user->orderBy($sort['users']['column'], $sort['users']['type']);
        }

        return $this->user->select($this->columns)->withCount([
            'orders',
            'orders as turnover' => function($query) { $query->select(DB::raw("SUM(price) as turnover")); },
            'orders as profit'   => function($query) { $query->select(DB::raw("SUM(profit) as profit")); },
            ])->get();
    }
}

条件返回true但记录未按其排序。我甚至试图在返回之前放$this->user->orderBy(),但它根本没有被吸引。它似乎根本就没用过。

以下是它运行的查询:

"select `id`, `email`, `pwd`, `name`, `addr`, `addr_detail`, `addr_no`, `city`, `zip`, `state`, `phone`, `company_number`, `vat_id`, `tax_id`, `unique_string`, `active`, `banned`, `admin`, (select count(*) from `orders` where `users`.`id` = `orders`.`user_id`) as `orders_count`, (select SUM(price) as turnover from `orders` where `users`.`id` = `orders`.`user_id`) as `turnover`, (select SUM(profit) as profit from `orders` where `users`.`id` = `orders`.`user_id`) as `profit` from `users` "

任何帮助表示赞赏。感谢

2 个答案:

答案 0 :(得分:1)

试试这个..

public function getAll()
    {
        $query = $this->user->select($this->columns)->withCount([
            'orders',
            'orders as turnover' => function($query) { $query->select(DB::raw("SUM(price) as turnover")); },
            'orders as profit'   => function($query) { $query->select(DB::raw("SUM(profit) as profit")); },
            ]);

        if (Session::has('sort')) 
        {
            $sort = Session::get('sort');
            if (isset($sort["users"]) && !empty($sort["users"]))
                $query = $query->orderBy($sort['users']['column'], $sort['users']['type']);
        }

        return $query->get();
    }

确保if (isset($sort["users"]) && !empty($sort["users"]))允许执行。

答案 1 :(得分:0)

试试这个..似乎orderBy没有反映到select的下一个查询..

public function getAll()
{
        $query = $this->user;
        if (Session::has('sort')) 
        {
            $sort = Session::get('sort');
            if (isset($sort["users"]) && !empty($sort["users"])) {
                $query = $query->orderBy($sort['users']['column'], $sort['users']['type']);
            }
        }

        return $query->select($this->columns)->withCount([
            'orders',
            'orders as turnover' => function($query) { $query->select(DB::raw("SUM(price) as turnover")); },
            'orders as profit'   => function($query) { $query->select(DB::raw("SUM(profit) as profit")); },
            ])->get();
    }