Here is the key thing...
// Searched on Google and got this load missing() used case.
Laravel's loadMissing() method is used to eager load a relationship on a model or a collection of models only if that relationship has not already been loaded.
-
withPivot() only applies when the relationship is loaded for the first time.
-
If groups.articles was already loaded earlier without the formula column, loadMissing() will not re-query or add new pivot fields.
-
Eloquent treats the relationship as already loaded, so this behavior fails silently by design.
I think you can define the 2 relationship one for without formula and other with formula
Like this ..
//Without formula relationship
public function articles(): BelongsToMany
{
return $this->belongsToMany(Article::class, 'group_article')
->using(GroupArticle::class)
->withPivot('rounding_rule', 'precision');
}
// With formula needed
public function articlesWithFormula(): BelongsToMany
{
return $this->belongsToMany(Article::class, 'group_article')
->using(GroupArticle::class)
->withPivot('formula', 'rounding_rule', 'precision');
}