PHP code example of plokko / querable-resource

1. Go to this page and download the library: Download plokko/querable-resource library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

plokko / querable-resource example snippets


 class TestQuerableResource extends \Plokko\QuerableResource\QuerableResource {
 
      protected function getQuery(): Illuminate\Database\Eloquent\Builder
      {
          return App\User::where('id','>',1); // Just a simple query to demonstrate functionality
      }
  }


Route::get('/test',function(){
    $qr = new TestQuerableResource();
    return $qr;
});

class TestQuerableResource extends \Plokko\QuerableResource\QuerableResource {
      protected
        $paginate = 30,//30 items per page
        /**
         * Allowed client-defined paginations,
         *      if null no client pagination is allowed
         *      if int set the maxium page size allowed
         *      if array only the page sizes listed in the array are allowed
         * @var int|array|null
         */
        $paginations = 100;
      protected function getQuery(): Illuminate\Database\Eloquent\Builder
      {
          return App\User::where('id','>',1); // Just a simple query to demonstrate functionality
      }
}

Route::get('/test',function(){
    $qr = new TestQuerableResource();
    $qr->paginate(10); // Set pagination to 10 items per page
    return $qr;
});

class TestQuerableResource extends \Plokko\QuerableResource\QuerableResource {
      protected
        $filteredFields = ['name','email']; // enable filtering for name and email columns
      protected function getQuery(): Illuminate\Database\Eloquent\Builder
      {
          return App\User::where('id','>',1); // Just a simple query to demonstrate functionality
      }
}

Route::get('/test',function(){
    $qr = new TestQuerableResource();
    return $qr;
});

class TestQuerableResource extends \Plokko\QuerableResource\QuerableResource {
      protected
        $filteredFields = [
		'n' => 'name', // query parameter n to field name
		'mail' => 'email', // query parameter mail to field email
	];
	//....
}

class TestQuerableResource extends \Plokko\QuerableResource\QuerableResource {
      protected
        $filteredFields = [
		'n' => [ //an can be used
			'field'=>'name', 
			'type'=>'like',
		],
		[
			'field'=>'email', // If no alias is specified field will be used as query parameter
			//comparaison type is optional
		]
	];
	//....
}

class TestQuerableResource extends \Plokko\QuerableResource\QuerableResource {
      protected
        $filteredFields = ['name','email'], // enable filtering for name and email columns
        $filterQueryParameter='filter';
      protected function getQuery(): Illuminate\Database\Eloquent\Builder
      {
          return App\User::where('id','>',1); // Just a simple query to demonstrate functionality
      }
}

class TestQuerableResource extends \Plokko\QuerableResource\QuerableResource {
	// No need to specify $filteredFields, we're using a custom filtering function

	//Override the default filtering function
    protected function filter(Builder &$query,array $filterValues){
		if(array_key_exists('name',$filterValues)){
			$query->where('name','LIKE','%'.$filterValues['name'].'%');//apply your filtering
		}
		//....
	}

	protected function getQuery(): Illuminate\Database\Eloquent\Builder
	{
		return App\User::where('id','>',1); // Just a simple query to demonstrate functionality
	}
}

class TestQuerableResource extends \Plokko\QuerableResource\QuerableResource {
  protected
   $useResource = \App\Http\Resources\UserResource::class;
  protected function getQuery(): Illuminate\Database\Eloquent\Builder
  {
	  return App\User::where('id','>',1); // Just a simple query to demonstrate functionality
  }
}