Giter VIP home page Giter VIP logo

Comments (5)

selahattinunlu avatar selahattinunlu commented on June 24, 2024

Firstly, thanks for proposal @robsontenorio :)
But I did not understand actually why do you need it, sorry. I will be glad if you explain using an example case.

from laravel-api-query-builder.

robsontenorio avatar robsontenorio commented on June 24, 2024

I have my "list all" methods encapsulated in Models. They are encapsulated, because sometimes i can do some custom queries, like: get relations , order by ... not only LIST ALL.

// Games.php - Chess, Mario , Kings ...

public function listAll($request){
     // return api query builder results
}

Other methods in here: save, delete, update ...

// Platforms.php - PS4, XboxOne

public function listAll($request){
     // return api query builder results
}

Other methods in here: save, delete, update ...

// Player.php - usernames ...

public function listAll($request){
     // return api query builder results
}

Other methods in here: save, delete, update ...

So in my endpoint mysite.com/top i would like to show the top records from some entities.

// RecentController.php - list recent records

public function index(Request $request){
     $player = Player::listAll($request);  // recent players: limit=5&order_by=id,desc
     $games = Game::listAll($request);    // recent games added: limit=20&order_by=id,desc
     $plataforms = Game::listAll($request); //recent plataforms added: limit=10&order_by=id,desc

     // return mounted json 
}

In this scenario above my "list all" method have different behavior depending on parameters. By using current implementation ($request object) i cant use custom filters, because all of them depends on the single $request object.

By, using "array of parameters" in QueryBuilder i can build custom queries. It would be an "untied implementation".

public function index(Request $request){

     // using custom params

     $player = Player::listAll(['limit' => 5, ....]);  // recent players: limit=5&order_by=id,desc
     $games = Game::listAll(['limit' => 20, ....]);    // recent games added: limit=20&order_by=id,desc
     $plataforms = Game::listAll(['limit' => 10, ....]); //recent plataforms added: limit=10&order_by=id,desc

     // return mounted json 
}

I'm currently using this approach. I've changed QueryBuilder.phpconstructor and simple changes in related classes.

This "breaking change" allow people to use your lib simple changing $request parameter to $request->all() :

new QueryBuilder(new User(), $request->all()))->build()->paginate();

Or in other custom scenario:

$params = []
new QueryBuilder(new User(), $params))->build()->paginate();

from laravel-api-query-builder.

robsontenorio avatar robsontenorio commented on June 24, 2024

QueryBuilder -> construct

public function __construct(Model $model, array $params = []) {
....
$this->uriParser = new UriParser($params);
....

UriParser -> construct

public function __construct(array $params = []) {
....
$this->uri = (count($params) > 0) ? '?' . http_build_query($params) : NULL;
....

from laravel-api-query-builder.

selahattinunlu avatar selahattinunlu commented on June 24, 2024

@robsontenorio I commented to pull request. (#19) Please check it :)

from laravel-api-query-builder.

selahattinunlu avatar selahattinunlu commented on June 24, 2024

@ili5 @robsontenorio

I created different solution. You can check it from here 2a85dea

I will documented it.

I hope you like new solution :)

By the way thanks for idea @ili5 and @robsontenorio

Shortly you can use it like this:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Unlu\Laravel\Api\QueryBuilder;
use Unlu\Laravel\Api\RequestCreator;
use App\User;

class UsersController extends Controller
{
    public function index()
    {
        $request = RequestCreator::createWithParameters([
            'name' => 'selahattin',
            'email' => '[email protected]'
        ]);

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

        return response()->json([
            'data' => $qBuilder->build()->get()
        ]);
    }
}

from laravel-api-query-builder.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.