This is in Laravel 5.1, but the principle should work for all previous versions.
The way I handled this was with two routes pointing to the same method, but with one end in the .json extension:
get('items', ['as' => 'items', 'uses' => ' ItemsController@index ']); get('items.json', ['as' => 'items', 'uses' => ' ItemsController@index ']);
Then inside my index() method:
$data = []; // your collection if ($this->request->ajax()) { return response()->json($data); // replace with actual JSON data } return view('items.index', compact('data'));
This allows you to use a dedicated URL for JSON responses, uses the same method and data, and never interferes with my return button.
source share