You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Now, after adding the #[AutoLazy] attribute, avatarUrl becomes lazy as expected.
However, it still triggers an automatic relationship load, occurring in NormalizedModel::fetchNewProperty.
So, how can we make avatarUrl truly lazy (i.e., avoid querying the relation unless it’s actually accessed)?
I’ve thought of one solution: use a custom from method.
Amazing! It works exactly as expected.
But this raises another issue: good practice in database querying says we should only select the fields we need—never use wildcards.
So let’s modify the query:
// AppServiceProvider
Model::shouldBeStrict(app()->isLocal());
// ----$user = App\Models\User::firstOrFail(['id']);
$vud = VUserData::from($user)->only('id', 'avatarUrl'); // We only need these two fieldsdump($vud);
Now $user only has the id, yet it can still access the avatarUrl through the hasOne relation.
However, this code doesn’t behave well:
In local mode, it throws an exception saying the name field doesn’t exist.
In production, it doesn’t throw but name becomes null, which causes a type error since name is non-nullable.
So should we just make the field nullable? That would avoid the error, and since we used only, name won’t appear in the final output anyway.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
Let’s start by defining some classes:
Now, the following code will silently auto-load the
avatarrelation:This happens because
avatarUrlis not a real attribute—it’s computed from a model relation.Now, after adding the
#[AutoLazy]attribute,avatarUrlbecomes lazy as expected.However, it still triggers an automatic relationship load, occurring in
NormalizedModel::fetchNewProperty.So, how can we make
avatarUrltruly lazy (i.e., avoid querying the relation unless it’s actually accessed)?I’ve thought of one solution: use a custom
frommethod.Amazing! It works exactly as expected.
But this raises another issue: good practice in database querying says we should only select the fields we need—never use wildcards.
So let’s modify the query:
Now
$useronly has theid, yet it can still access theavatarUrlthrough thehasOnerelation.However, this code doesn’t behave well:
namefield doesn’t exist.namebecomesnull, which causes a type error sincenameis non-nullable.So should we just make the field nullable? That would avoid the error, and since we used
only,namewon’t appear in the final output anyway.I also came up with another approach:
Then:
This seems to reduce boilerplate nicely.
Or?
So—am I missing something here? Is there a better approach? Thanks.
Relation: #1071 #378
Beta Was this translation helpful? Give feedback.
All reactions