salamwaddah/laravel-relation-parser
| Install | |
|---|---|
composer require salamwaddah/laravel-relation-parser |
|
| Latest Version: | v1.4.0 |
| PHP: | >=8.1 |
| Laravel: | 10 – 13 |
| Older releases: | 8 · 9 |
| License: | MIT |
| Last Updated: | Apr 13, 2026 |
| Links: | GitHub · Packagist |
Installation
composer require salamwaddah/laravel-relation-parser
| Version | Laravel | PHP |
|---|---|---|
| 1.x | 10, 11, 12, 13 | >= 8.1 |
| 0.3.x | 8, 9 | >= 7.3 |
Usage
Allow your application to load any model relation in the response without the need of changing your API controllers.
Pass a comma separated relations to your http request.
Loading relations
Add a with param to your HTTP request
/users?with=orders,posts,anyOtherRelation
Loading relation counts
Add a with_count param to your HTTP request
/users?with_count=orders,anyOtherRelation,anotherRelationToCount,blaBlaBla
Loading relations and counts
/users?with=orders&with_count=orders
In your controller
use SalamWaddah\RelationParser\LoadsRelations;
use Illuminate\Http\Request;
class UsersController extends Controller
{
use LoadsRelations;
public function index(Request $request)
{
$users = User::query()
// .. your query logic here
->get();
// this line adds the relations/counts
$this->loadRelations($users, $request);
// return your results however you like
return response()->json($users);
}
}
Response example
[
{
"id": 1,
"name": "Salam",
"orders_count": 2,
"orders": [
{
"id": 2,
"product": "something",
"price": 100
},
{
"id": 1,
"product": "something else",
"price": 150
}
]
},
{
"id": 2,
"name": "Naren",
"orders_count": 1,
"orders": [
{
"id": 3,
"product": "something",
"price": 100
}
]
}
]
Customize
If with or with_count params are used for something else in your application then you can customize those params
in loadRelations() method.
$this->loadRelations($users, $request, 'customWithParam', 'custom_with_count_param');
Testing
composer test
Related Packages
Powerful PHP database abstraction layer (DBAL) with many features for database s...
Laravel Serializable Closure provides an easy and secure way to serialize closur...
Cli error handling for console/command-line PHP applications.
Version History
| Version | Released | PHP | Laravel | License |
|---|---|---|---|---|
| v1.4.0 | >=8.1 | ^10.0 || | MIT | |
| v1.3.0 | >=8.1 | ^10.0 || | MIT | |
| v1.2.0 | >=8.1 | ^10.0 || | MIT | |
| 1.1.0 | >=8.1 | ^10.0 || | MIT | |
| 1.0.0 | >=8.1 | ^10.0 | MIT | |
| 0.3.1 | >=7.3 | ^8.0 || | MIT | |
| 0.3.0 | >=7.3 | ^8.0 || | MIT | |
| 0.2.1 | >=7.3 | ^8.0 || | MIT | |
| 0.2.0 | >=7.3 | ^8.0 || | MIT | |
| 0.1.3 | >=7.3 | ^8.0 | MIT | |
| 0.1.2 | >=7.3 | ^8.0 | MIT | |
| 0.1.1 | >=7.3 | ^8.0 | MIT | |
| 0.1.0 | >=7.3 | ^8.0 | MIT | |
| 0.0.4 | >=7.3 | ^8.0 | MIT | |
| 0.0.3 | >=7.3 | ^8.0 | MIT | |
| 0.0.2 | >=7.3 | ^8.0 | MIT | |
| 0.0.1 | >=7.3 | ^8.0 | MIT |