Skip to content

Using Custom Request

Selahattin Ünlü edited this page Jun 14, 2017 · 3 revisions

Where did the idea come from?:


Sometimes you want to use custom parameters instead of directly give Request object to query builder.

You need to create a custom request to make this. "RequestCreator" makes easier it and covers package's all features. (using !=, <, >= etc.)

So, let's create a custom request object like the request object that will be created when call this uri: /api/users?name=selahattin&age!=23&created_at!=[null]

<?php

use Unlu\Laravel\Api\RequestCreator;
use Unlu\Laravel\Api\QueryBuilder;
use App\User;

class UserController extends Controller
{
  public function index()
  {
    $request = RequestCreator::createWithParameters([
      'name' => 'selahattin',
      'age' => '!=18',
      'created_at' => '!=[null]'
    ]);

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

Using by operators

=

RequestCreator::createWithParameters([
  'columns' => 'name,age,city_id',
  'includes' => 'city',
  'name' => 'sela*',
  'limit' => '3',
  'order_by' => 'id,desc'
]);

<, >, <=, >=

RequestCreator::createWithParameters([
  'age' => '<18',
  'other_column' => '>23',
  'other_column2' => '<=26',
  'one_other_col' => '>=28'
]);

whereIn, whereNotIn

RequestCreator::createWithParameters([
  'age' => '[]=18&age[]=23', // whereIn
  'other' => '[]!=17&other[]!=26' //whereNotIn
]);

null

RequestCreator::createWithParameters([
  'age' => '!=[null]',
  'updated_at' => '=[null]'
]);