Architect is used for dynamically creating new structures for API resource relationships. Sounds confusing and pretentious?
Imagine you have a resource Book
with a related resource Author
.
Book 1-----n Author
This is how related resources are loaded by default using embedded mode.
{
"books":[
{
"id":1,
"author_id":1,
"title":"How to save the world from evil",
"pages":100,
"author":{
"id":1,
"name":"Linus torvalds"
}
},
{
"id":2,
"author_id":2,
"title":"How to take over the world",
"pages":100,
"author":{
"id":2,
"name":"Richard stallman"
}
}
]
}
With Architect now you can load related resources using ids
mode and
sideloading
mode
Only load the IDs of the related resource.
{
"books":[
{
"id":1,
"author_id":1,
"title":"How to save the world from evil",
"pages":100,
"author":1
},
{
"id":2,
"author_id":2,
"title":"How to take over the world",
"pages":100,
"author":2
}
]
}
Hoist the related resources into the global scope and leave behind the IDs using the ID mode resolver.
{
"author":[
{
"id":1,
"name":"Linus torvalds"
},
{
"id":2,
"name":"Richard stallman"
}
],
"books":[
{
"id":1,
"author_id":1,
"title":"How to save the world from evil",
"pages":100,
"author":1
},
{
"id":2,
"author_id":2,
"title":"How to take over the world",
"pages":100,
"author":2
}
]
}
Architect works with normal array's (collections and resources), Illuminate\Support\Collection
and Illuminate\Database\Eloquent\Model
.
<?php
$books = Book::with('Author')->get();
$architect = new \Bulwark\Architect\Architect;
$parsed = $architect->parseData($books, [
'author' => 'sideload' // can also be embed or ids (embed is default)
], 'books');
Bulwark\LaravelController gives nice convenience methods to define the Architect relationships in query parameters.
composer require bulwark/architect ~1.0
This package is compliant with PSR-1, PSR-2 and PSR-4. If you notice compliance oversights, please send a patch via pull request.
$ phpunit
Please see CONTRIBUTING for details.
The MIT License (MIT). Please see License File for more information.