1

So I have part of an API up and running, but am stuck on a few things. I am working with Laravel's REST controllers and am loving the ability to use methods such as Response::eloquent($query); But, in using this:

  1. How should I handle query parameters (e.g id=27&order_by=timestamp), if the resource that I am getting is joining several tables?
  2. Should I get the values from the database, and then construct an array with only the certain fields that I want to display for API access? And how would this then work with query parameters?

2 Answers 2

4

First off I would recommend that you watch Teach a Dog to REST[Link broken] for more information about formatting your REST routes.

As for your questions:

  1. You can handle input fields through the Input::get() and Input::all() methods as mentioned in laravel input & cookies docs. You might also have to validate incoming data with laravel's Validator class.
  2. Say you acquired the id and order_by fields from the input as mentioned in your example:

    $id = Input::get('id'); $order_criteria = Input::get('order_by');

using Fluent Query Builder

DB::table('dbtable')->where('id', '=', $id)->order_by($order_criteria, 'desc')->first();

// Using first() since we are pretty sure that we are getting only one result out, for a different criteria where more than a result could be returned you might want to use ->get() instead.

Sign up to request clarification or add additional context in comments.

5 Comments

Hey mate! I have followed Apigee's blog closely and have already learned a lot from their awesome videos. I am already doing some of what you mentioned. I guess my question really is, how do I include the query parameters in a way that is flexible for different resources? Should I extend the base controller and write some sort of function that handles query params? How would you recommend I do this?
Oh well i suppose i am not getting what you really need help with, whether how to handle the request or the formatting of the params in your URLs ? maybe you can provide some sort of examples to begin with.
Should I be constructing an array myself with only the values that I want returned in the response?
@Sneaksta you ever figure this out, or find a good resource to learn from?
@kmmathis Yep! What I ended up doing is writing a function in the base controller class that is called something like 'queryParams' and then run it in my controllers, wrapping my eloquent queries. E.g: $this->queryParams(User::where('user', '=', 'me'))->first(); This enabled me to handle the query object in the queryParams class and tag on any extra queries, according to what is in the Input::get() object.
3

I released a package for this.

You can check this out https://github.com/selahattinunlu/laravel-api-query-builder

For wiki page: https://github.com/selahattinunlu/laravel-api-query-builder/wiki

This package creates query with url parameters.

Example:

/api/users?name=se*&age!=18&order_by=age,asc&limit=2&columns=name,age,city_id&includes=city

And resulting query

Users::with(['city'])->select(['name', 'age', 'city_id'])
                     ->where('age', '!=', 18)
                     ->where('name', 'like', 'se%')
                     ->orderBy('age', 'asc')
                     ->take(2)

Usage:

$queryBuilder = new QueryBuilder(new User, $request);

return response->json([
  'data' => $queryBuilder->build()->paginate(),
  .
  .
]);

That's all.

1 Comment

@OliverBayes-Shelton Yes I'm not maintaining it anymore. I can give push permission if anyone wants to maintain it

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.