0

I've got the following code in my api.php file:

Route::group(['prefix' => 'v1', 'middleware' => 'auth:api'], function() {
    // Return user information
    Route::get('/user', function(Request $request) {
        return $request->user();
    });

    // Return  user runs
    Route::get('/runs', function(Request $request) {
        $user = $request->user('api');

        return $user->runs();
    });

    Route::get('/runs/{id}', function(Request $request, $id) {
        $user = $request->user('api');

        return $user->run($id);
    });

    // Return user profiles
    Route::get('/profiles', function(Request $request) {
        $user = $request->user('api');

        return $user->profiles();
    });

});

API Call

I'm testing the API using PostMan:

GET - http://srp.local/api/v1/runs/1

Authentication is done using a bearer token and this is working fine, I'm however having problems with the /runs/{id} call, all I want is for the call to return the run associated with that ID (basically a filtered version of the /runs call), but I'm struggling to get the parameter of the ID accross.

Currently, the code above works, but when trying to perform the aforementioned call, all it does is return the output for /user and doesn't return the run with the given ID.

I'm also not sure if I'm doing my routing optimally using the method above so any help would be appreciated - thank you.

2
  • Can you show your call data? i.e. from browser console or from wherever you are making the request. Your routes look fine. Commented Aug 25, 2019 at 9:09
  • @Rehmat I've updated the question with the call. Commented Aug 25, 2019 at 9:15

1 Answer 1

1

Models can be resolved by using route-model binding, checkout the following example:

 Route::get('/runs/{run}', function(Request $request, Run $run) {
        $user = $request->user('api');


        return $user->run($run);
    });

More info: https://laravel.com/docs/5.8/routing#implicit-binding

Using controllers is preferred over using callbacks in routes. Controllers help separating code and allows caching. Checkout the following example: https://laravel.com/docs/5.8/controllers#defining-controllers

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.