Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow auto parsing the fieldsets #261

Merged
merged 5 commits into from
Dec 9, 2024

Conversation

Mohammad-Alavi
Copy link
Contributor

Summary

Allow the fieldsets to be auto parsed, and configurable similar to the way the include and exclude features are handled.

Copy link
Member

@freekmurze freekmurze left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you also add an example to the docs. Make sure it is easily understandable by providing an example URL and what the results would be;

README.md Show resolved Hide resolved
@freekmurze freekmurze merged commit 3134ca2 into spatie:main Dec 9, 2024
1 check passed
@freekmurze
Copy link
Member

Thanks!

@Mohammad-Alavi
Copy link
Contributor Author

Could you also add an example to the docs. Make sure it is easily understandable by providing an example URL and what the results would be;

Of course 🤗 . I'll do it later today or tomorrow 🖖

@Mohammad-Alavi Mohammad-Alavi deleted the auto-parse-fieldsets branch December 9, 2024 12:29
@Mohammad-Alavi
Copy link
Contributor Author

Mohammad-Alavi commented Dec 12, 2024

@freekmurze I was going to add some examples to the docs as you requested but while I was at it I noticed that the other semi-related laravel-fractal features are also not documented in this package. I believe that is the case because laravel-fractal is just a very convenient wrapper around fractalistic and just delegate all the method calls to fractalistic without modifying them in any way.
So I suggest we document those features (e.g, include, exclude and fieldsets) in fractalistic docs as we are already pointing the user to go to fractalistic docs to learn about all the available methods in laravel-fractal.

This was my previous attemp at adding those missing docs to fractalistic but it was closed.
Now that you have more context, if you agree with what I said, just give me the word and I'll have another go on updating the fractalistic docs. Otherwise let me know if you still want this feature docs to be added here, in this package.

@skrskr
Copy link

skrskr commented Dec 16, 2024

We have issue after last update to 6.3.0
php: 8.3
laravel: 10.10

Error

{
    "exception": {
      "class": "ErrorException",
      "message": "Array to string conversion",
      "code": 0,
      "file": "/var/task/vendor/spatie/fractalistic/src/Fractal.php:277"
    },
   "aws_request_id":  "23a192bf-94de-4a39-a076-518413a24e89"
}

Code

# Model 
class Registration extends Model
{
    use HasFactory;
   use SoftDeletes;

    protected $fillable = [
        'organization_id',
        'event_id',
        'persona_id',
        'user_id',
        'source',
        'barcode',
        'fields',  // json field
        'checks_log', // json
        'attendance_log', //json
    ];

    protected $casts = [
        'fields' => 'array',
    ];

}

# Transformer

use App\Models\Registration;
use League\Fractal\TransformerAbstract;

class RegistrationTransformer extends TransformerAbstract
{
    /**
     * List of resources to automatically include
     */
    protected array $defaultIncludes = [
        //
    ];

    /**
     * List of resources possible to include
     */
    protected array $availableIncludes = [
    ];

    /**
     * A Fractal transformer.
     */
    public function transform(Registration $registration): array
    {
        return [
            'id' => $registration->id,
            'organization_id' => $registration->organization_id,
            'event_id' => $registration->event_id,
            'persona_id' => $registration->persona_id,
            'user_id' => $registration->user_id,
            'source' => $registration->source,
            'barcode' => $registration->barcode,
            'fields' => $registration->fields,
            'checks_log' => $registration->checks_log,
            'attendance_log' => $registration->attendance_log,
        ];
    }
}

@Mohammad-Alavi
Copy link
Contributor Author

Mohammad-Alavi commented Dec 16, 2024

@skrskr could you please create a separate issue for this so we can continue the conversation there?
I believe this is caused by the conflict between the expected request_key, fields, and the fields key in your transformer. I suspect the same will happen for the include and exclude too.
You can try and change the fieldsets request_key to something other than fields or disable this feature all together.

I created a PR to disable fieldsets by default.

@skrskr
Copy link

skrskr commented Dec 16, 2024

Hello @Mohammad-Alavi
Thank you so much.
I tried to create a separate issue but issues tab disabled on this repo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants