【问题标题】:Laravel. Query Builder groupBy拉拉维尔。查询生成器 groupBy
【发布时间】:2018-01-01 03:00:30
【问题描述】:

所以我有一个名为“orders”的表,该表具有receipt_id,以将其与名为“receipts”的表链接起来

我想对具有相同receipt_id的订单进行分组,所以在我的控制器中,我使用了这个代码;

        $orders = DB::table('orders')
        ->where('status', 0)
        ->groupBy('receipt_id')
        ->get();

    return $orders;

但不幸的是,它只返回第一个。我有一种感觉,我不应该使用 groupBy。

这是它当前返回的内容;

{
id: 1,
item: "12",
qty: 123,
price: "1823.00",
subtotal: "123.00",
receipt_id: 123,
status: 0,
created_at: null,
updated_at: null
}

编辑:

我只想查询 receipts 表,这是我的刀片现在的样子,并评论了我想要完成的任务。

@forelse($orders as $order)
        <div class="col-md-4">
            <div class="panel panel-default">
                <div class="panel-heading text-center">
                    {{$order->receipt_id}}
                </div>

                <div class="panel-body text-center">
                    <table class="table table-bordered table-responsive">
                        <thead>
                            <th>Order</th>
                            <th>Quantity</th>
                        </thead>

                        <tbody>

                            //all orders that has the same receipt_id will be showed here.
                            <tr>
                                <td>{{$order->item}}</td>
                                <td>{{$order->qty}}</td>
                            </tr>
                        </tbody>
                    </table>
                </div>

                <div class="panel-footer clearfix">
                    <button type="submit" class="btn btn-success pull-right" style="margin-right: 10px;">
                        <i class="fa fa-check-circle" aria-hidden="true"></i> Served
                    </button>
                </div>
            </div>
        </div>
        @empty

        @endforelse

编辑:收据和订单的数据库结构。

收据;

Schema::create('receipts', function (Blueprint $table) {
            $table->increments('id');
            $table->decimal('total');
            $table->decimal('vatable');
            $table->decimal('vat');
            $table->decimal('vat_exempt');
            $table->decimal('vat_zero');
            $table->decimal('amount_due');
            $table->decimal('cash');
            $table->decimal('change_due');
            $table->integer('receipt_id');
            $table->timestamps();
        });

订单;

Schema::create('orders', function (Blueprint $table) {
            $table->increments('id');
            $table->string('item');
            $table->integer('qty');
            $table->decimal('price');
            $table->decimal('subtotal');
            $table->integer('receipt_id');
            $table->boolean('status');
            $table->timestamps();
        });

【问题讨论】:

  • 如果您已定义关系,则很容易获得order 的所有receipts。在此处查看文档laravel.com/docs/5.4/eloquent-relationships
  • 你的查询似乎是对的,如果我错了,你的所有ordersstatus 1 都有相同的receipt_id,如果我错了,你的一些示例数据会让你成为一个查询
  • @SagarGautam 我只想查询“订单”表。也许这很难想象,所以我编辑了我的原始帖子,展示了我的刀片的样子。
  • @ArunpandianM 我编辑了主帖以显示查询返回的内容。
  • 在你的数据库中检查这个:select * from orders where status != 0; 并告诉我们

标签: php laravel controller


【解决方案1】:

不知何故,我设法解决了我的问题,当然我会分享我的代码。 :)

控制器;

$receipt_ids = DB::table('orders')
            ->where('status', 0)
            ->orderBy('receipt_id', 'asc')
            ->groupBy('receipt_id')
            ->get();

        $orders = DB::table('orders')
            ->where('status', 0)
            ->orderBy('receipt_id', 'asc')
            ->get();

        return view('kitchens.index', compact('receipt_ids','orders'));

刀片;

@extends('layouts.app')

<div class="panel panel-default">
    <div class="panel-body" style="overflow: scroll; height:85vh;">
        <div class="row">
            @forelse($receipt_ids as $receipt_id)
                <div class="col-md-4">
                    <div class="panel panel-default">
                        <div class="panel-heading text-center">
                            {{$receipt_id->receipt_id}}
                        </div>

                        <div class="panel-body">
                            <table class="table table-bordered table-responsive">
                                <thead>
                                    <th>Name</th>
                                    <th>Quantity</th>
                                </thead>

                                <tbody>
                                @foreach($orders as $order)
                                    @if($receipt_id->receipt_id == $order->receipt_id)
                                        <tr>
                                            <td>{{$order->item}}</td>
                                            <td>{{$order->qty}}</td>
                                        </tr>
                                    @endif
                                @endforeach
                                </tbody>
                            </table>
                        </div>

                        <div class="panel-footer clearfix">
                            <button type="submit" class="btn btn-success pull-right" style="margin-right: 10px;">
                                <i class="fa fa-check-circle" aria-hidden="true"></i> Served
                            </button>
                        </div>
                    </div>
                </div>
            @empty

            @endforelse
        </div>
    </div>
</div>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-08
    • 2020-06-24
    • 1970-01-01
    • 2015-04-13
    • 2014-11-12
    • 2015-08-24
    • 2017-09-19
    相关资源
    最近更新 更多