1. Go to this page and download the library: Download gregorip02/restql 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/ */
gregorip02 / restql example snippets
use App\Author;
use Illuminate\Http\Request;
Route::get('/authors', function (Request $request) {
$authors = Author::take(25)->get();
return ['data' => compact('authors')];
});
use Illuminate\Database\Eloquent\Model;
use Restql\Traits\RestqlAttributes;
class Author extends Model
{
use RestqlAttributes;
// ...
}
namespace App\Restql\Authorizers;
use Restql\Authorizer;
final class AuthorAuthorizer extends Authorizer
{
// ...
}
namespace App\Restql\Authorizers;
use Restql\Authorizer;
final class AuthorAuthorizer extends Authorizer
{
/**
* Can get one or more author resources.
*
* @param array $clausules
* @return bool
*/
public static function get(array $clausules = []): bool
{
// You could replace this with permission checks or validations.
return true;
}
}
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
class Author extends Model
{
/**
* Get the author articles.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function articles(): {} // Bad
public function articles(): HasMany {} // Good
}
use Restql\Restql;
use Illuminate\Http\Request;
Route::get('/restql', fn (Request $request) => Restql::resolve($request));
// Assuming that the parent model we want to obtain is the author's data.
// The variable $query represents the query constructor of the parent model,
// in this example, the Author model.
$query->select(['name'])->with([
'articles' => static function (Relation $relation) {
$relation->select(['title']);
}
]);