【问题标题】:Laravel Datepicker And Eloquent QueryLaravel 日期选择器和雄辩的查询
【发布时间】:2015-01-02 20:48:08
【问题描述】:

我正在创建一个页面,我可以在其中使用来自 jqueryui.com 的日期范围、日期选择器,而且我对 laravel 框架非常陌生。

我有如下雄辩的查询,

public function orderbydate()
{

    $order =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('sum(w.total_invoiced) as total_invoiced'), 
                DB::Raw('sum(w.shipping_amount) as shipping_amount')))
            ->where('qty_canceled','=','0')
            ->where('status','!=','canceled')
            ->get();

    $orderbydate = 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('DATE(w.created_at) days'), 
                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('days')
            ->orderBy('s.created_at')
            ->paginate(10);

        return View::make('sales_flat_orders.orderbydate', compact('order','orderbydate'));
}

注意:此功能将显示模板上的所有数据

另一个功能是

public function orderbydate1()
    {
        $startDate = Input::get('w.created_at');
        $endDate = Input::get('w.created_at');

        $order1 =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('sum(w.total_invoiced) as total_invoiced'), 
                    DB::Raw('sum(w.shipping_amount) as shipping_amount')))
                ->where('qty_canceled','=','0')
                ->where('status','!=','canceled')
                ->get();

        $orderbydate1 = 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('DATE(w.created_at) days'), 
                    DB::Raw('sum(w.total_invoiced) as total_invoiced'),
                    DB::Raw('sum(w.shipping_amount) as shipping_amount')))
                ->whereBetween('w.created_at',array($startDate,$endDate))
                ->where('qty_canceled','=','0')
                ->where('status','!=','canceled')
                ->groupBy('days')
                ->orderBy('s.created_at')
                ->paginate(10);

            return View::make('sales_flat_orders.result', compact('order','orderbydate1'));
    }

我有一个名为 OrderByDAte.blade.php

的模板

按日期销售

    {{ Form::open() }}

<label for="from">From</label>
<input type="text" id="from" name="from">
<label for="to">to</label>
<input type="text" id="to" name="to">
<input type="submit" value="Submit" class="btn btn-info btn-block">
        {{ Form::close() }}
<script>
  $(function() {
    $( "#from" ).datepicker({
      defaultDate: "+1w",
      changeMonth: true,
      numberOfMonths: 3,
      onClose: function( selectedDate ) {
        $( "#to" ).datepicker( "option", "minDate", selectedDate );
      }
    });
    $( "#to" ).datepicker({
      defaultDate: "+1w",
      changeMonth: true,
      numberOfMonths: 3,
      onClose: function( selectedDate ) {
        $( "#from" ).datepicker( "option", "maxDate", selectedDate );
      }
    });
  });
  </script>

也是显示表格的循环。

现在

**result.blade.php** which displays the data. 
<table class="table table-striped table-bordered table-hover" id="dataTables-example">
    <thead>
        <tr class="odd gradeX"> 
        <th>DATE</th>
      <th>Items Ordered</th>
      <th>Subtotal</th>
      <th>Tax Amount</th>
      <th>Discount Amount</th>
      <th>Row Total</th>
      <th>Refunded</th>
     <th>Total Invoiced</th>
     <th>Shipping Amount</th>
    </tr>
        </thead>

  @foreach($orderbydate1 as $s)
 <tbody>
  <tr class="odd gradeX">
     <td>{{date("d F, Y",strtotime($s->days))}}</td>
     <td>{{ round($s->qty_ordered, 2) }}</td>
     <td>{{round($s->subtotal,2)}}</td>
     <td>{{round($s->tax_amount,2)}}</td>
     <td>{{round($s->discount_amount,2)}}</td>
     <td>{{round($s->row_total,2)}}</td>
     <td>{{round($s->amount_refunded,2)}}</td>
     <td>{{round($s->total_invoiced,2)}}</td>
     <td>{{round($s->shipping_amount,2)}}</td>
  </tr>

 @endforeach

注意:我认为我的提交按钮不起作用。不知道为什么?

现在我想使用 datepicker 按日期过滤它,在我的路线中我知道我必须使用 post 并且在提交日期后我必须获取该范围内的数据表。

这里如何使用 $startDate 和 $endDate。在我雄辩的询问中。我必须为 startDate 输入 'w.created_at' 字段,还为 $endDate 输入 'w.created_at' 字段。 我想我必须像这样在我的函数中使用外观输入。

$startDate = Input::get('w.created_at');
$endDate = Input::get('w.created_at');

但是这个功能不起作用。

我目前正在使用引导日期选择器。但如果你认识其他人,那么你可以给我举个例子。

现在有两个模板用于

Route::get('orderbydate', array('as'=>'orderbydate', 'uses'=>'SalesFlatController@orderbydatesales'))

将所有数据放入我的模板中。

对于我使用的帖子

Route::get('result', array('as'=>'result', 'uses'=>'SalesFlatController@orderbydate'))

注意:我收到类似 Missing 1 参数的错误。

任何人都可以帮助我知道我哪里出错了吗?

【问题讨论】:

    标签: laravel-4 eloquent jquery-ui-datepicker laravel-routing


    【解决方案1】:

    你需要改变:

    public function orderbydate($startDate, $endDate)
    

    到:

    public function orderbydate()
    

    当您从 Input::get() 获取这些值时。只有当这些变量来自路径中的变量时,您才会将这些变量放入函数调用中,即:

    Route::get('result/{startDate}/{endDate}', array('as'=>'result', 'uses'=>'SalesFlatController@orderbydate'))
    

    【讨论】:

    • 嘿@ceejayoz,我在帖子中添加了更多信息。你能调查一下吗?
    • 其实我在点击提交后得到methodnotallowedexception。
    • @CoolD 你得到 MethodNotAllowedExceptions 因为你 POSTing 到 GET 路由。
    • 是的@ceejayoz。对,我明白了。你能解释一下 whereBetween 是如何工作的吗?我的意思是这里没有接管输入。我看到了 sql 查询,它显示空白 where w.created between '' and '' like this
    • 您在哪里看到的查询?如果您打印出查询日志,它将有? 占位符。 whereBetween 的实现很好,假设你给它 MySQL 的日期格式 YYYY-MM-DD HH:ii:ss
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-26
    • 2021-07-23
    • 1970-01-01
    • 1970-01-01
    • 2017-11-11
    相关资源
    最近更新 更多