No need GraqhQL. This is power query controll for your model.
await axios.get('/api/my-models', {
params: {
/* Select and Append control */
select: 'id,name', // <-- select columns
with: 'items.subItems', // <-- append with relation
withCount: 'items', // <-- append relation count
limit: 10, // <-- limit count
/* Response type control */
count: true, // <-- count response
// or
paginate: 10, // <-- pagination count
page: 1, // <-- pagination page
// or
// default: model list
}
})
composer require laravel-expansions/query-control-model
Publish to /stubs
template files.
php artisan vendor:publish --tag=expansion-qcm-stubs
Next create model.
php artisan make:model SomeModel --controller --resource
use QueryControlModel on your model.
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use LaravelExpansions\QueryControlModel\Traits\QueryControlModel;// <-- add
class MyModel extends Model
{
use HasFactory;
use QueryControlModel;// <-- set
public function scopeQueryFilter($query)
{
return $query;// add your local scopes
}
}
And set scopes on your controller methods.
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Models\MyModel;
class MyModelController extends Controller
{
public function index()
{
return MyModel
::queryControll()
->queryFilter()
->queryGet();
}
public function show($id)
{
return MyModel
::queryControl()
->queryFilter()
->findOrFail($id);
}
...
}