【问题标题】:How to view data based on date range using laravel and mysql如何使用 laravel 和 mysql 根据日期范围查看数据
【发布时间】:2017-10-17 05:36:10
【问题描述】:

路线:

Route::post('dategraph','Chatbot\TrackerController@dategraph');

控制器:

public function dategraph(Request $request)
{
    $dategraph = DiraStatistics::all()->whereBetween('date_access', [$from, $to])->get();

    $dates = $dategraph('date_access');

    return view('AltHr.Chatbot.graph', compact('dates'));
}

查看:

<form id="form-project" role="form" action="{{action('AltHr\Chatbot\TrackerController@dategraph')}}" autocomplete="off" method="POST">

          {{csrf_field()}}
          <!-- <canvas id="myChart" width="150" height="50"></canvas> -->

            <div class="form-group-attached">
              <div class="row">
                  <div class="col-lg-6">
                      <div class="form-group form-group-default required" >
                          <label>From</label>
                          <input type="date" class="form-control" name="from" required>
                      </div>
                  </div>
                  <div class="col-lg-6">
                      <div class="form-group form-group-default required" >
                          <label>To</label>
                          <input type="date" class="form-control" name="to">
                      </div>
                  </div>
              </div>
            </div>
            <button class="btn alt-btn-black btn-xs alt-btn pull-right" type="submit">Next</button>
</form>

大家好,我正在尝试按照我编写的代码查看所选日期的数据。但我得到一个错误。我写对了吗?还是我错过了什么?

【问题讨论】:

  • [$from, $to] 在代码中看不到这些值。
  • @DanyalSandeelo 对不起,你是什么意思?
  • $from 和 $to 的值,你需要从 $request 中获取它们为$request-&gt;input()['from']
  • 你同时使用all()get()
  • 你遇到了什么错误?

标签: php mysql database laravel date


【解决方案1】:

您没有$from 变量。 您需要从请求中提取已发布的变量。

get() 方法将返回对象的Collection。例如,您可以通过拔出列并将其翻转为平面数组toArray()

$dategraph = DiraStatistics::whereBetween(
    'date_access', 
    [
        $request->get('from'),
        $request->get('to')
    ]
)->get();
$dates = $dategraph->pluck('date_access')->toArray();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多