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

withTrashed() calls on BelongsTo are no longer allowed since version 0.6.6 #690

Closed
jdrieghe opened this issue Oct 19, 2020 · 4 comments · Fixed by #692
Closed

withTrashed() calls on BelongsTo are no longer allowed since version 0.6.6 #690

jdrieghe opened this issue Oct 19, 2020 · 4 comments · Fixed by #692
Labels
enhancement New feature or request

Comments

@jdrieghe
Copy link
Contributor

  • Larastan Version: 0.6.6
  • --level used: 2

Description

Phpstan/Larastan complains about ->withTrashed() calls on BelongsTo relationship definitions since version 0.6.6.
This was fine in version 0.6.5 and lower.

Laravel code where the issue was found

    public function category(): BelongsTo
    {
        return $this->belongsTo(Category::class)->withTrashed();
    }

    public function supplier(): BelongsTo
    {
        return $this->belongsTo(Supplier::class)->withTrashed();
    }
 ------ --------------------------------------------------------------------------------------------------------------------------- 
  Line   Invoice/ReadModels/Mysql/EnteredInvoice.php                                                                                
 ------ --------------------------------------------------------------------------------------------------------------------------- 
  93     Call to an undefined method Illuminate\Database\Eloquent\Relations\BelongsTo<Invoicr\Categories\Category>::withTrashed().  
  98     Call to an undefined method Illuminate\Database\Eloquent\Relations\BelongsTo<Invoicr\Suppliers\Supplier>::withTrashed().   
 ------ --------------------------------------------------------------------------------------------------------------------------- 
@canvural
Copy link
Collaborator

Hi,

I think in previous versions this was working by "mistake". Unknown method calls on relation was treated as DummyMethodReflection With the recent refactorings this is not the case anymore.

So I'd say this is a future request to support SoftDelete trait calls on relations.

@canvural canvural added the enhancement New feature or request label Oct 19, 2020
@WouterSioen
Copy link

@canvural any hints on how we could implement this support for SoftDelete? We expected larastan to get into the RelationForwardsCallsExtension extension, but it didn't seem to get into that code.

@canvural
Copy link
Collaborator

canvural commented Oct 20, 2020

@WouterSioen Are checking it in the hasMethod method? It should get in there.

The problem in general is; here we just try to find the method in the builder. So before that line there needs to be a check like

if ($relatedModel->hasMethod($methodName)->yes()) {
    return true;
}

Then in getMethod we need to check it again and return correct method reflection.

if ($relatedModel->hasMethod($methodName)->yes()) {
    return new EloquentBuilderMethodReflection(.....);
}

@canvural
Copy link
Collaborator

Actually the check needs to happen if we can't find it in the builder. That makes more sense 👍

@canvural canvural linked a pull request Oct 20, 2020 that will close this issue
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants