【问题标题】:Laravel group by with Elequent modelLaravel group by 与 Eloquent 模型
【发布时间】:2017-07-31 23:52:09
【问题描述】:

我正在使用 Laravel 和 Eloquent ORM,在为我的表开发控制器、模型和视图之后,我需要提取聚合信息,但我不知道这是最好的方法还是最干净的“Laravel”方法.

我有一个 db table 像这个例子:

Schema::create('order_items', function (Blueprint $table) {
        $table->increments('id');
        $table->timestamps();
        $table->integer('order_id')->unsigned();
        $table->foreign('order_id')->references('id')->on('orders')->onDelete('cascade');
        $table->string('item_description');
        $table->integer('item_qty');
        $table->status('item_status');
    });

详细数据可以是:

2   2017-02-28 12:48:07 2017-02-28 12:48:31 1   ProductB    2   NEW
4   2017-02-28 12:48:17 2017-02-28 12:48:17 1   ProductC    3   NEW
29  2017-03-10 10:49:47 2017-03-10 10:49:47 1   ProductC    23  CLOSED
40  2017-03-10 10:49:47 2017-03-10 10:49:47 1   ProductB    2   SHIPPED
1   2017-02-28 11:04:28 2017-02-28 11:29:10 3   ProductA    1   NEW
28  2017-03-10 10:49:47 2017-03-10 10:49:47 3   ProductB    22  CLOSED
39  2017-03-10 10:49:47 2017-03-10 10:49:47 3   ProductA    1   SHIPPED
5   2017-02-28 14:36:54 2017-02-28 14:36:54 6   ProductD    4   NEW
6   2017-02-28 14:37:01 2017-02-28 14:37:01 6   ProductD    5   NEW
30  2017-03-10 10:49:47 2017-03-10 10:49:47 6   ProductD    24  CLOSED
41  2017-03-10 10:49:47 2017-03-10 10:49:47 6   ProductC    3   SHIPPED

在控制器中我使用范围

public function home()
{
  $onlynew = Orderitem::onlynew ();
  return view('home', compact('onlynew '));
}

模型是

public function scopeonlynew ($query) {
return \DB::select('SELECT item_description, sum(item_qty) qty 
                    FROM   order_items 
                    WHERE item_status = ? 
                    GROUP BY item_description',['NEW']);
}

在视图中我可以通过这种方式访问​​数据

<div class="row">
 <ul>
  @foreach ($onlynew as $newitem)
   <li>{{$newitem->item_description}} - {{$newitem->qty}}</li>
  @endforeach
 </ul>
</div>

是否可以使用类似于以下示例的语法或框架不允许使用构建器的此类查询?

return $query->where('item_status', '=', 'NEW')
         ->sum('item_qty')
         ->groupBy('item_description')
         ->orderBy('item_description');

感谢您的任何帮助或建议。

【问题讨论】:

    标签: laravel eloquent


    【解决方案1】:

    我个人更喜欢使用 DB::table 语句。

    public function scopeonlynew () {
        return DB::table('order_items')->where('item_status', '=','NEW')->get();
    }
    

    让我知道这是否适合你!

    【讨论】:

    • 我同意你的观点,你建议的 DB::table 语句有效,但这是一个返回详细结果的“过滤器”,如何分组和聚合?谢谢。
    • return DB::table('order_items')->where('item_status','=','NEW')>groupBy('item_qt')->having('account_id', ' >', 5);
    • 这不是一个范围。范围应该返回给定的 $query 对象。这只会创建一个新查询。因此,如果您尝试Orderitem::where('item_description', '=', 'ProductA')-&gt;onlynew(),它将无法按预期工作。它只会返回所有新项目,而不仅仅是新的 productA 项目。
    • @jfadich:你说得对,我使用 Orderitem::onlynew() 直接从模型 Orderitem 调用我的范围 onlynew() 我在范围内添加过滤器。谢谢。
    • @DeHazelaar 这不是范围。作用域将 Builder 作为第一个参数,并在操作后将其返回。您的范围正在返回一个集合。
    【解决方案2】:

    当您使用sum() 方法时,它会执行查询并仅返回总和。您在这里想要的是获得总和和描述,因此您必须自己构建选择。这就是“Laravel”方式的样子。

    控制器:

    public function home()
    {
        $onlynew = Orderitem::onlyNew()->get();
        return view('home', compact('onlynew '));
    }
    

    型号:

    public function scopeOnlyNew($query) 
    {
        return $query->where('item_status', '=', 'NEW')
            ->selectRaw('sum(item_qty) as qty, item_description')
            ->groupBy('item_description')
            ->orderBy('item_description');
    }
    

    请注意,查询范围的构建略有不同。 Query scopes are designed to allow you to group commonly use query constraints into a single method 然后在您正在构建的任何查询中重用这些约束。它们应该始终返回与它们相同的查询对象,而不是像 DB::select() 这样创建一个新的查询对象。

    【讨论】:

    • selectRaw 正是我正在寻找的东西,也是我想念的文档。谢谢。
    猜你喜欢
    • 2022-01-23
    • 2018-03-29
    • 1970-01-01
    • 1970-01-01
    • 2015-10-29
    • 2021-07-04
    • 2016-04-22
    • 1970-01-01
    • 2017-04-03
    相关资源
    最近更新 更多