Lomkit / laravel-rest-api

Generate Api in seconds
https://laravel-rest-api.lomkit.com/
MIT License
297 stars 18 forks source link

Resource relation MorphTo order #120

Open devcodemarcos opened 2 weeks ago

devcodemarcos commented 2 weeks ago

Laravel Rest Api Version

2.7

Laravel Version

10.14.1

PHP Version

8.1.3

Database Driver & Version

No response

Description

The order of my inverse polymorphic relationships affects the result obtained when displaying the columns of each model. For example: I have this relationship

final class Person extends Model
{
    public function personable(): MorphTo
    {
        return $this->morphTo();
    }
}

final class PersonResource extends RestResource
{
    public static $model = Person::class;

    public function relations(RestRequest $request): array
    {
        return [
            MorphTo::make('personable', PhysicalPeopleResource::class), ---> placed 1st
            MorphTo::make('personable', MoralPeopleResource::class),
        ];
    }
}

and I get this result

{
    "current_page": 1,
    "data": [
        {
            "id": 1,
            "code": "GCPR0001",
            "person_id": 167,
            "created_at": "2024-06-12T16:32:57.000000Z",
            "updated_at": "2024-06-12T16:32:57.000000Z",
            "deleted_at": null,
            "person": {
                "id": 167,
                "person_type_id": 1,
                "personable_id": 1,
                "personable_type": "App\\Models\\PhysicalPeople",
                "name": null,
                "last_name": null,
                "surname": null,
                "birthday": null,
                "gender": null,
                "rfc": null,
                "identification_type_id": null,
                "business_name": null,
                "address": null,
                "created_at": "2024-06-12T16:32:57.000000Z",
                "updated_at": "2024-06-12T16:32:57.000000Z",
                "personable": { ---> all columns PhysicalPeople model
                    "id": 1,
                    "name": "Mario",
                    "last_name": "Vilchis",
                    "surname": "Nava",
                    "gender": "No especificado",
                    "birthday": null,
                    "nationality_id": null,
                    "civil_status": "Soltero(a)",
                    "religion": null,
                    "occupation": null,
                    "created_at": "2024-06-12T16:32:57.000000Z",
                    "updated_at": "2024-06-12T16:32:57.000000Z",
                    "deleted_at": null
                }
            }
        }
    ],
    "from": 1,
    "last_page": 1,
    "per_page": 50,
    "to": 3,
    "total": 3,
    "meta": []
}

But when I change the order of my relationships I get a different result

final class Person extends Model
{
    public function personable(): MorphTo
    {
        return $this->morphTo();
    }
}

final class PersonResource extends RestResource
{
    public static $model = Person::class;

    public function relations(RestRequest $request): array
    {
        return [
            MorphTo::make('personable', MoralPeopleResource::class),
            MorphTo::make('personable', PhysicalPeopleResource::class),  ---> placed 2nd
        ];
    }
}

and I get this result

{
    "current_page": 1,
    "data": [
        {
            "id": 1,
            "code": "GCPR0001",
            "person_id": 167,
            "created_at": "2024-06-12T16:32:57.000000Z",
            "updated_at": "2024-06-12T16:32:57.000000Z",
            "deleted_at": null,
            "person": {
                "id": 167,
                "person_type_id": 1,
                "personable_id": 1,
                "personable_type": "App\\Models\\PhysicalPeople",
                "name": null,
                "last_name": null,
                "surname": null,
                "birthday": null,
                "gender": null,
                "rfc": null,
                "identification_type_id": null,
                "business_name": null,
                "address": null,
                "created_at": "2024-06-12T16:32:57.000000Z",
                "updated_at": "2024-06-12T16:32:57.000000Z",
                "personable": { ---> columns that match in both models
                    "id": 1,
                     "name": "Mario",
                    "created_at": "2024-06-12T16:32:57.000000Z",
                    "updated_at": "2024-06-12T16:32:57.000000Z",
                    "deleted_at": null
                }
            }
        }
    ],
    "from": 1,
    "last_page": 1,
    "per_page": 50,
    "to": 3,
    "total": 3,
    "meta": []
}

Only columns that match in both models are shown

Steps To Reproduce

  1. Create a polymorphic People table
  2. Create two models of person types, physical and moral, both with different columns (id, created_at and updated_at are in both models)
  3. When creating the MorphTo relationship from the People resource, the order in which each relationship is called is important since it depends on whether or not it shows you the corresponding columns of each model
GautierDele commented 2 weeks ago

Hello there, I'm quite on a big feature to implement Laravel Scout, i'm gonna have a look just after 😄, thanks for your patience