【问题标题】:Laravel groupby Week Eloquent queryLaravel groupby Week Eloquent 查询
【发布时间】:2014-12-25 04:46:28
【问题描述】:

作为 Laravel 的新手,我对日期格式化程序和刀片模板有些疑问。

我写了一个以正确方式执行的查询, 以下是我的 Eloquent SQL 查询。

public function orderbyweek()
    {

        $orderbyweek = DB::table('sales_flat_order_items as s')
                ->leftJoin('sales_flat_orders as w', 'w.entity_id','=','s.order_id')
                ->select(array(DB::Raw('sum(s.amount_refunded) as amount_refunded'),
                    DB::Raw('sum(s.row_total) as row_total'),
                    DB::Raw('sum(s.discount_amount) as discount_amount'),
                    DB::Raw('sum(s.tax_amount) as tax_amount'), 
                    DB::Raw('sum(s.qty_ordered) as qty_ordered'),
                    DB::Raw('sum(w.subtotal) as subtotal'),
                    DB::Raw('WEEK(w.created_at) week'), 
                    DB::Raw('sum(w.total_invoiced) as total_invoiced'),
                    DB::Raw('sum(w.shipping_amount) as shipping_amount')
                    ))
                ->where('qty_canceled','=','0')
                ->where('status','!=','canceled')
                ->groupBy('week')
                ->orderBy('w.created_at')
                ->get();

            return View::make('sales_flat_orders.orderbyweek', compact('orderbyweek'));


    }

这向我显示了我想要一周的表格,间隔为 7 天。 那么在我的模板中我需要做什么?如何为此添加日期格式化程序?我在这里做了一些事情:

<table width="100%">
    <thead>

        <th>DATE</th>
        </thead>

  @foreach($orderbyweek as $s)

  <tr>
     <td>{{date("d F, Y",strtotime($s->week))}}</td>

  </tr>

 @endforeach
</table>

我执行了如下的 sql 查询:

select sum(s.amount_refunded) as amount_refunded, sum(s.row_total) as row_total, sum(s.discount_amount) as discount_amount, sum(s.tax_amount) as tax_amount, sum(s.qty_ordered) as qty_ordered, sum(w.subtotal) as subtotal, WEEK(w.created_at) week, sum(w.total_invoiced) as total_invoiced, sum(w.shipping_amount) as shipping_amount from `sales_flat_order_items` as `s` left join `sales_flat_orders` as `w` on `w`.`entity_id` = `s`.`order_id` where `qty_canceled` = '0' and `status` != 'canceled' group by `week` order by `s`.`created_at` asc

我哪里错了?

我得到了 ordebydate,orderbyyear 也是。但不是按周排序

请帮帮我。

【问题讨论】:

    标签: php sql laravel laravel-4 eloquent


    【解决方案1】:

    这是我用于月份分组的,我认为这也适用于一周。

        $data = \Invoice::select(   
                            DB::raw("MONTHNAME(STR_TO_DATE(MONTH(date), '%m')) as month"),
                            DB::raw('SUM(g_total) as total_invoice_amnt'),
                            DB::raw('MONTH(date) as no_mnth')
                        );
                        foreach ($filters as $filter) {
                            if ($filter[2] != '') {
                                $data->where($filter[0], $filter[1], $filter[2]);
                            }
                        }
        $data = $data->groupBy("month")
                     ->OrderBy('no_mnth','ASC')
                     ->get()
                     ->toArray();
    
        return $data;
    

    【讨论】:

    • 这实际上已经完成了..但是间隔7天没有得到如何做,即。一周。
    • 在你的查询中你错过了这样的东西 DB::Raw('WEEK(w.created_at) week'), => DB::Raw('WEEK(w.created_at) as week' ),
    • y 这个双箭头(=>) 是 der,U 肯定会出现语法错误,它不会在那里工作。
    • DB::Raw('WEEK(w.created_at) week'),行替换为 DB::Raw('WEEK(w.created_at) as week'),我表示你哥们
    • 是的,明白了,而且我认为刀片模板中使用的格式化程序是错误的??如何格式化?例如:7th,July-14th,july...可能是从 7th,jul,sun-14th, july,sat 开始。而在7的区间内,
    猜你喜欢
    • 1970-01-01
    • 2017-10-22
    • 2020-06-24
    • 1970-01-01
    • 2015-02-22
    • 2017-06-05
    • 1970-01-01
    • 1970-01-01
    • 2015-02-13
    相关资源
    最近更新 更多