Skip to content

Model::shouldBeStrict(true); #134

@ultrono

Description

@ultrono

Within my app service provider I'm calling:

Model::shouldBeStrict(! $this->app->isProduction());

I'm then calling a query as follows:

$place = Place::query()
    ->withDistance('location', new Point(1, 1, 4326))
    ->first();

In a HTTP resource I'm then accessing $this->distance.

I then get an exception (MissingAttributeException).

When I comment out shouldBeStrict, everything works fine. Is there any way around this?

Activity

MatanYadaev

MatanYadaev commented on Mar 22, 2025

@MatanYadaev
Owner

Can you send a PR with a failing test that reproduces this issue? I tried reproducing this issue without success.

ultrono

ultrono commented on Mar 22, 2025

@ultrono
Author

Sorry no PR, my example describes my exact use case. I'm assuming this is because the withDistance method returning an attribute not on my model, which is what Laravel's built in strict mode prevents.

Changing my coilumn name to "distance" or adding a third argument to withDistance may be worth a try i.e.

$place = Place::query()
    ->withDistance('location', new Point(1, 1, 4326), 'location')
    ->first();

With this I can do $this->location in model, which in theory will pass Laravel's strict mode. Will report back.

MatanYadaev

MatanYadaev commented on Mar 22, 2025

@MatanYadaev
Owner

As far as I know, the error is thrown only when the data is not in the $model->attributes array. In this case, it should be, because it was returned from the database. Maybe you load the distance attribute conditionally?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @ultrono@MatanYadaev

        Issue actions

          Model::shouldBeStrict(true); · Issue #134 · MatanYadaev/laravel-eloquent-spatial