yepwoo / laravel-parser
Easily load model relations / accessories / attributes from frontend without changing your API
Installs: 2
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 0
pkg:composer/yepwoo/laravel-parser
Requires
- php: ^7.4|^8.0
- illuminate/database: ^8.0 || ^9.0
- illuminate/http: ^8.0 || ^9.0
Requires (Dev)
- mockery/mockery: ^1.4.2
- phpunit/phpunit: ^8.5.8|^9.3.3
This package is auto-updated.
Last update: 2025-09-29 02:52:19 UTC
README
Installation
composer require yepwoo/laravel-parser
Usage
Allow your application to load any model relation & attributes in the response without the need of changing your API controllers. Pass a comma separated relations/attributes to your http request.
Loading relations
Add with param to your HTTP request
/posts?with=comments,tags,anyOtherRelation
Loading Attributes / Accessors
Add append param to your HTTP request
/posts?append=my_custom_attribute
How to use
- Go to your controller and call
use Yepwoo\LaravelParser\LoadParser - Create new object from
LoadParserclass and passrequestanddata->new LoadParser($request, $data) - If you want to load
relationsuseloadRelationfunction. - If you want to load
attributes/accessorsuseloadAttributesfunction.
Here's a full example
<?php
namespace App\Http\Controllers;
use App\Http\Resources\PostResource;
use App\Models\Post;
use Illuminate\Http\Request;
use Yepwoo\LaravelParser\LoadParser;
class PostController extends Controller
{
public function index(Request $request)
{
$posts = Post::query()
// .. your query logic here
->get();
// new instance
$parser = new LoadParser($request, $posts);
// load relations
$parser->loadRelations();
// load attributes
$parser->loadAttributes();
return PostResource::collection($posts);
}
}
Response Example
[
{
"id": 1,
"title": "Aperiam.",
"comments": [
{
"id": 6,
"body": "Consequuntur dolores voluptates qui minima. Enim modi quam perferendis iste eum.",
"post_id": 1,
"created_at": "2022-06-02T15:03:36.000000Z",
"updated_at": "2022-06-02T15:03:36.000000Z"
},
{
"id": 8,
"body": "Eaque quaerat nobis voluptatum fugiat a quisquam. Qui consequuntur officia eum ut blanditiis.",
"post_id": 1,
"created_at": "2022-06-02T15:03:36.000000Z",
"updated_at": "2022-06-02T15:03:36.000000Z"
}
],
"custom_attr": "post_1"
},
{
"id": 2,
"title": "Incidunt.",
"comments": [
{
"id": 5,
"body": "Sequi beatae atque placeat excepturi distinctio. Illum et eum quisquam natus quaerat qui eos.",
"post_id": 2,
"created_at": "2022-06-02T15:03:36.000000Z",
"updated_at": "2022-06-02T15:03:36.000000Z"
},
{
"id": 7,
"body": "Corrupti eveniet enim et. Qui veritatis consectetur voluptas. Natus esse et rem aut.",
"post_id": 2,
"created_at": "2022-06-02T15:03:36.000000Z",
"updated_at": "2022-06-02T15:03:36.000000Z"
},
{
"id": 12,
"body": "Perferendis fugit harum magnam et fugiat id beatae. Sit sed ipsam omnis magnam vel aut.",
"post_id": 2,
"created_at": "2022-06-02T15:03:36.000000Z",
"updated_at": "2022-06-02T15:03:36.000000Z"
}
],
"custom_attr": "post_2"
}
]
Resources
Will make posts & videos soon.
TODO
Support filters