2

In my Laravel project, I have a dashboard that displays data on a weekly basis. Now, I need to provide a way for users to toggle between weeks, and to have the dashboard data update accordingly.

Ideally, I would use query parameters to achieve this—something like domain.dev/dashboard?start=2017-01-02&end=2017-01-08. Then, if a user hit a back button, they could return the week previous (e.g. domain.dev/dashboard?start=2016-12-26&end=2017-01-02), and so on.

Here's my current route:

Route::get('/dashboard/weekly', [
    'as' => 'providerDashboardIndex', 'uses' => 'DashboardController@indexWeekly'
]);

Here's the corresponding controller:

public function index()
{
    $user = Auth::user();
    $endDate = Carbon::today();
    $startDate = Carbon::today()->subDays(7);

    return view('providers.dashboard.index', compact('user', 'startDate', 'endDate') );
}

When a user hits the route initially, how would I add those query parameters to my URL? I understand date creation, but unsure how to pass those dates through.

1 Answer 1

1

First of all, create string dates:

$endDate = Carbon::today()->toDateString();
$startDate = Carbon::today()->subDays(7)->toDateString();

If you want to use exactly /dashboard?start=2017-01-02&end=2017-01-08 URI format, dashboard route should like like this:

Route::get('dashboard', ....

Then you'll be able to create a link in a view:

{{ url('dashboard?start=').$startDate.'&end='.$endDate }}

To get dates in a controller use request() helper:

public function showDashboard()
{
    $startDate = request('startDate');
    $endDate = request('endDate');
Sign up to request clarification or add additional context in comments.

1 Comment

?start=').$startDate.'&end='.$endDate should be ?startDate=').$startDate.'&endDate='.$endDate for the controller to work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.