A simple way, to be able to build a query into a Laravel model, through a http request.
Via Composer
$ composer require matteomeloni/laravel-rest-ql
Extends your model with Matteomeloni\LaravelRestQl\LaravelRestQl
<?php
namespace App\Models;
use Matteomeloni\LaravelRestQl\LaravelRestQl;
class Book extends LaravelRestQl
{
//
}
and into the controller you can use restQl
scope to make a new query
<?php
namespace App\Http\Controllers;
use App\Models\Book;
class BookController extends Controller
{
public function index()
{
$books = Book::restQL()
->get();
return response()->json($books);
}
}
There are three ways to build a query.
<?php
namespace App\Http\Controllers;
use App\Models\Book;
class BookController extends Controller
{
public function index()
{
$data = [
'search' => ''
'columns' => [],
'filters' => [],
'sorts' => []
];
$books = Book::restQL($data)->get();
return response()->json($books);
}
}
You can pass data with http url parameters
or http request header
.
In this case the format is JSON and you can use base64 for encoding data.
<?php
//Route: /api/books?select=[]&filters=[]&sorts=[]&search=''
namespace App\Http\Controllers;
use App\Models\Book;
class BookController extends Controller
{
public function index()
{
$books = Book::restQL()->get();
return response()->json($books);
}
}
URL: /api/books
Parameter: columns
/api/books?columns=["title", "description"]
Searches for the string in all columns of the table or fillable attribute.
URL: /api/books
Parameter: search
/api/books?search=laravel
Avaialable comparison operators: =
!=
<
<=
>
>=
like
not like
in
not in
between
not between
null
not null
URL: /api/books
Parameter: filters
/api/books?filters=[{"column":"category_id","operator":"=","value":1}]
/api/books?filters=[{"column":"category_id","operator":"between","value":[1,10]}]
/api/books?filters=[{"column":"category_id","operator":"in","value":[1,3,5]}]
/api/books=filters=[{"column":"category_id","operator":"=","value":1},{"column":"title","operator":"like","value":"PHP"}]
It is possible to concatenate two or more conditions with or logic.
URL: /api/books
Parameter: filters
/api/books?filters=[{"column":"category_id","operator":"=","value":1},{"column":"category_id","operator":"=","value":3,"boolean":"or"}]
Multiple "where" clauses can be grouped in parentheses to achieve logical grouping in the query.
URL: /api/books
Parameter: filters
/api/books?filters=[{"column":"title","operator":"like","value":"PHP"},[{"column":"category_id","operator":"=","value":1},{"column":"category_id","operator":"=","value":3,"boolean":"or"}]]
URL: /api/books
Parameter: sorts
/api/books?sorts=[{"column":"title"}]
/api/books?sorts=[{"column":"title", "direction":"desc"}]
/api/books?sorts=[{"column":"title"},{"column":"category_id","direction":"desc"}]
-
php 8.0 or later
-
Laravel 9
$ composer test
Please see the changelog for more information on what has changed recently.
Please see contributing.md for details and a todolist.
If you discover any security related issues, please email matteomelonig@gmail.com instead of using the issue tracker.
MIT. Please see the license File for more information.