0

I'm getting the following error:

GET http://backend.test/test?last_name=DOE 404 (Not Found)

When trying the following GET method:

My GET request in Vue.JS:

    testMethod: _.debounce(async function () {
      await axios.get('test', { params: { last_name: 'DOE' } })
        .then((response) => {
          console.log(response);
        })
        .catch((error) => {
          console.log(error);
        });
    }, 50),

The route in Laravel:

Route::get('test/{params}', 'TestController@filter');

The method in my Laravel's TestController.php (not passed):

    public function filter($params)
    {
        Log::info($params);
    }

What's wrong with it ?

=== EDIT ===

If I rewrite the route like this:

Route::get('test/{params?}', 'TestController@filter');

Then I get the following error in Laravel logs:

[2020-04-26 21:28:48] local.ERROR: Too few arguments to function App\Http\Controllers\TestController::filter(), 0 passed and exactly 1 expected {"userId":1,"exception":"[object] (ArgumentCountError(code: 0): Too few arguments to function App\Http\Controllers\TestController::filter(), 0 passed and exactly 1 expected at /Users/me/code/backend/app/Http/Controllers/TestController.php:10)

5
  • what does it say when you add ? to params like, {params?} - you are not sending params, just sending query string. Commented Apr 26, 2020 at 21:25
  • @Ersoy I have just updated my post to reply your question. Commented Apr 26, 2020 at 21:33
  • Please update your controller method to accept Request to and remove params - and try to log with $request->toArray(); Commented Apr 26, 2020 at 21:34
  • 1
    That works perfect. Thanks a lot! Maybe you can post it as an answer ? Commented Apr 26, 2020 at 22:46
  • glad to hear that, i will post an answer, thanks. Commented Apr 26, 2020 at 22:53

1 Answer 1

2

Since Route::get('test/{params}', 'TestController@filter'); route expects params to be mandotary; adding ? to params will make it optional.

Route::get('test/{params?}', 'TestController@filter');

To fetch last_name, it is required to update controller method as following;

use Illuminate\Http\Request;

class TestController
{
    public function index(Request $request)
    {
        $request->get('last_name');
        // actions
    }
}
Sign up to request clarification or add additional context in comments.

Comments

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.