anonymous
anonymous

Reputation: 257

How to view data based on date range using laravel and mysql

Route:

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

Controller:

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'));
}

View:

<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>

Hi guys, so im trying to view the data from the selected dates as the code ive written. But im getting an error. Did i write it correctly? or am i missing something?

Upvotes: 1

Views: 1142

Answers (1)

Unamata Sanatarai
Unamata Sanatarai

Reputation: 6637

You do not have a $from variable. You need to pull out posted variables from the request.

The method get() will return a Collection of objects. You can, for example, turn it to a flat array by plucking the column and turning it toArray()

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

Upvotes: 1

Related Questions