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

Model::shouldBeStrict(true); #134

Open
ultrono opened this issue Feb 24, 2025 · 3 comments
Open

Model::shouldBeStrict(true); #134

ultrono opened this issue Feb 24, 2025 · 3 comments

Comments

@ultrono
Copy link

ultrono commented Feb 24, 2025

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?

@MatanYadaev
Copy link
Owner

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

@ultrono
Copy link
Author

ultrono commented Mar 22, 2025

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
Copy link
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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants