0

i am willing to know which is the best practice to use parameters for developing an api

//url
http://localhost:8000/api/my_orders/1/10/1

//route
Route::get('/my_orders/{user_id}/{limit}/{page}', 'ApiControllers\OrderController@getOrdersByUser');

//method
public function getOrdersByUser($user_id, $limit, $page)
{
    //logic using $user_id, $limit, $page
}

or

//url
http://localhost:8000/api/my_orders?user_id=1&&limit=5&&page=1

//route
Route::get('/my_orders', 'ApiControllers\OrderController@getOrdersByUser');

//method
public function getOrdersByUser(Request $request)
{
    //logic using $request->user_id, $request->limit, $request->page
}

this api is for mobile applications and for front end Vue application

thank you

2 Answers 2

1

Laravel has pagination built in, it will check for page and perpage query string arguments using paginate() or something that uses Paginator related methods for fetching a subset of your data.

So when you have ?page=1&perpage=20 and you use paginators, they will pick these up automatically.

For REST API endpoints, try and make the urls as descriptive as possible and based on your models.

// get a list of orders
GET /api/orders 

// get a list of orders belonging to user 1
GET /api/orders?user_id=1

// get a paginated list of 20 orders belonging to user 1
GET /api/orders?user_id=1&page=1&perpage=20

You are calling your endpoint my_orders, which basically says it will return orders owned by the authenticated user. So, in my opinion, it wouldn't make sense here to include a user_id argument.

// get a list of orders owned by the authenticated user
GET /api/my_orders

You can use my_orders, but more descriptive will be to use a url like:

// get a list of orders owned by user
GET /api/users/{user_id}/orders

Edit:

In your case, you probably want to create a UserOrderController

public function index($user_id)
{
    // first fetch user, if fetch fails show error
    $user = User::findOrFail($user_id);

    // maybe add some code here to check if the authenticated user is allowed to view this user's orders

    // return paginated orders
    return $user->orders()->paginate();
}

And you would need to define the relations in the models:

App/User.php

public function orders()
{
    // assuming orders table has a column user_id
    return $this->hasMany(Order::class);
}

App/Order.php

// inverse relation
public function user()
{
    return $this->belongsTo(User::class);
}
Sign up to request clarification or add additional context in comments.

1 Comment

thank you for your time, really appreciate it, if this is the case GET /api/users/{user_id}/orders, to get the data i will have to use relationships right?
1

I use 1st way for only those parameters which are associated with DB, like user_id here, because it reduces 1 step for me to get the specific data from DB of that particular ID. for more dynamic url laravel will go to this route even when you supply the wrong values and you have to apply the regix to avoid that. Secondly, URLs like 2/20/4 wouldnt make any sense and hard to understand. So the best way in my opinion is the second way.

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.