Questions tagged [thephpleague-fractal]
27 questions
18
votes
3 answers
Laravel 5.5 API resources for collections (standalone data)
I was wondering if it is possible to define different data for item resource and collection resource.
For collection I only want to send ['id', 'title', 'slug'] but the item resource will contain extra details ['id', 'title', 'slug', 'user',…

Dorin Niscu
- 721
- 1
- 9
- 26
11
votes
2 answers
Can I use transformers to transform data coming from API rather than from database?
I have been using laravel to build my APIs. I use transformers to tranform data from model object.
Now instead of database, I have a response coming from an API as the data source and I want to transform that data back to a user, but I am unable to…

Ajeesh
- 5,650
- 8
- 30
- 52
10
votes
2 answers
Eloquent: API Resources vs Fractal
Quick question, what is the difference between Eloquent: API Resources and Fractal?
To me, it looks like the same thing?

Oliver Bayes-Shelton
- 6,135
- 11
- 52
- 88
6
votes
1 answer
Laravel Fractal does not return meta from include relationship
This is my PostTransformer where I have included relationship
public function includeComments(Post $post)
{
if (($post->is_paid == 1 && $post->haspaid == 1) || ($post->author == $this->params) || ($post->is_paid == 0)){
$comments =…

samina Tuladhar
- 61
- 2
4
votes
2 answers
Fractal transformer with different Serializers for nested items and collections
To change the JSON output send by a Laravel 5.4 RESTful API, I make use of the Fractal package by thephpleague. Because pagination will be added in the future, it is important that collections make use of the default DataArraySerializer and single…

Sam
- 1,303
- 3
- 23
- 41
3
votes
1 answer
Laravel fractal transformers - not getting includes
I am using fractal package in laravel 5.3. I have a player model who has hasMany relationship with videos, so one player has many videos.
Player model:
public function videos()
{
return $this->hasMany(Video::class);
}
Video model:
public…

Leff
- 1,968
- 24
- 97
- 201
3
votes
2 answers
Laravel 5.1 and Fractal: including pivot table data on the transformer
Tables: contact, company and a relationship table with a custom pivot attribute company_contact (company_id, contact_id, is_main)
Company and Contact have a many to many relationship (belongsTo on both models).
Expected output when I retrieve the…

braindamage
- 2,226
- 4
- 24
- 33
2
votes
1 answer
Parsing includes for nested tranformers
I'm trying to figure out how I can parse includes for nested transformers, given the scenario below:
I have a controller which sets my ArraySerializer, parses includes for the OrderTransformer and then creates the data:
$data = (new Manager())
…

MylesK
- 3,349
- 2
- 9
- 31
2
votes
1 answer
Group collection by created_at and file relation
I have this relation:
Each user has gallery and gallery hasMany files.
Two users can have a gallery of the same date.
This is the output JSON response:
User1 {
galleries: {
created_at: 23.04.2019.
files: [
{
path: "path/to/file.jpg"
…

Zeljko
- 61
- 3
2
votes
1 answer
Laravel / League Fractal returns an empty array instead of empty object
I'm having a problem with Fractal library. I'm using Spatie\laravel-fractal library which is just wrapper for League\fractal. So...
I want to return an empty object instead of an empty array.
public function includeDocumentType(Question…

Buglinjo
- 2,067
- 1
- 15
- 26
2
votes
1 answer
Conditional Attributes in Fractal laravel transformers
I'm using Fractal laravel package as a presentation and transformation layer for complex data output.
I wrote a UserTransformer like this :
public function transform(User $user)
{
return [
'user_id' =>…

Ahmad Badpey
- 6,348
- 16
- 93
- 159
1
vote
0 answers
How to transform a model using php (laravel) fractal to get data grouped by a condition?
I have a table as follows :-
+----+-------------+------------+--------------+
| id | employee_id | contact_no | is_preferred |
+----+-------------+------------+--------------+
| 1 | 001 | 9138975999 | 1 |
| 2 | 001 |…

Tintin
- 475
- 1
- 5
- 16
1
vote
0 answers
Sort a resource collection in Fractal by an included primitive
I'm using the PHP Fractal library for my Laravel API responses. My model is a Post that has many Comments. What I want to do is to get all posts sorted by the amount of comments it has received in the last X days. Basically this API call:
GET…

Horrorente
- 314
- 3
- 13
1
vote
1 answer
yajra datatable fractal remove data key from included attributes
I want to use laravel-datatables-fractal for transforming server-side response using Fractal.
First I wrote a transformer for Product model like this :
class ProductTransformer extends TransformerAbstract
{
protected $availableIncludes = [
…

Ahmad Badpey
- 6,348
- 16
- 93
- 159
1
vote
1 answer
use fractal to decode from array to custom PHP object
using League/fractal, i am attempting to transform data from array to my PHP object...in the following way
final class StatusDeserializer extends AbstractTransformer
{
public function transform(Status $status)
{
return new…

Edwin O.
- 4,998
- 41
- 44