Rai
Rai

Reputation: 193

Laravel search by dates not working

I need to search by date. My route definition:

Route::get('/calendar/days?day={date}', 'HomeController@getDays');

enter image description here

In the controller method I have this:

public function getDays($date)
{
    $articles = Article::where('published_at','=',$date)->get();
    dd($articles);
}

When you click on the link on the image below I get a NotFoundHttpException.

The link is generated using JS. Could this be the problem?

Upvotes: 1

Views: 2164

Answers (1)

Bogdan
Bogdan

Reputation: 44526

You should not be defining query string parameters in the route definition. For a detailed explanation on why that is see this answer. So in your case you have two options:

1. Remove the ?day={date} from the definition:

Route::get('/calendar/days', 'HomeController@getDays');

And in your controller access the request input parameter like so:

use Illuminate\Http\Request;

...

public function getDays(Request $request)
{
    $date = $request->input('date');
    $articles = Article::where('published_at', '=', $date)->get();
}

2. Modify your route definition to something like this:

Route::get('/calendar/days/{date}', 'HomeController@getDays');

The controller code you have now needs no change in this case, however the link you generate via JavaScript would need to look like this:

<a href="/calendar/days/2015-04-09">9</a>

Upvotes: 1

Related Questions