selahattinunlu / laravel-api-query-builder

Laravel & Lumen Api Query Builder Package
332 stars 66 forks source link

What is the best way to override 'orderBy' configuration? #8

Closed macias67 closed 7 years ago

macias67 commented 7 years ago

I have a table with a primary key like this "model_id", I do not using "id", so what is the best way to override the array configuration (config/api-query-builder.php)?

GustavoHekel commented 7 years ago

I fixed this by commenting $this->orderBy = config('api-query-builder.orderBy'); on the QueryBuilder constructor class

selahattinunlu commented 7 years ago

Hi @Macias67 and @GustavoHekel Firstly, I'm sorry, I'm late.

There is multiple way for this.

1. You can change configuration file.

return [

    'limit' => 15,
    'orderBy' => [
        [
            'column' => 'id',
            'direction' => 'desc'
        ]
    ],
    'excludedParameters' => [],
];

to

return [

    'limit' => 15,
    'orderBy' => [],
    'excludedParameters' => [],
];

So, there is no default configuration anymore.

2. Create new QueryBuilder class that it extends base QueryBuilder like this:

class YourQueryBuilder extends QueryBuilder
    {
        public function __construct(Model $model, Request $request)
       {
           parent::__construct($model, $request);
           $this->orderBy = [];
       }
    }

I hope I could help you.