barryvdh / laravel-ide-helper

IDE Helper for Laravel
MIT License
14.23k stars 1.16k forks source link

"SoftDeletingScope" methods in all Eloquent Models #413

Closed Stalinko closed 3 years ago

Stalinko commented 7 years ago

All Eloquent models which use SoftDeletes trait accept methods from SoftDeletingScope:

  1. forceDelete
  2. restore
  3. withTrashed
  4. withoutTrashed
  5. onlyTrashed

Usage example: InventoryItem::withTrashed()->where('unit_id', $unit->id)->update(['unit_id' => 1]); Equipment::where('name', 'Stick')->onlyTrashed()->all();

Currently Laravel doesn't provide any way to typehint these methods in PHPStorm (and other smart IDEs if any...). We can solve this in ide-helper. I have some ideas how to do this, but I want to discuss with you which is better.

  1. Explicitly add a blank Illuminate\Database\Eloquent\Model class with PHPDoc where these methods described like:
namespace Illuminate\Database\Eloquent {
    /**
     * Class Model
     *
     * @package Illuminate\Database\Eloquent
     * @static \Illuminate\Database\Eloquent\Builder withTrashed()
     * ... the rest of methods ...
     */
    abstract class Model {}
}
  1. Add this PHPDoc before \Eloquent generated in _ide_helper.php file. Though I don't know if there is a good way if implementing this.

  2. Add this PHPDoc with artifical blank trait

namespace Illuminate\Database\Eloquent {
    trait SoftDeletes {}
}

What do you think guys and especially @barryvdh ?

UksusoFF commented 7 years ago

Duplicate of https://github.com/barryvdh/laravel-ide-helper/issues/371 but really needed.

anasred commented 4 years ago

Any Solution for withTrashed() on relationship?

Exception: Call to undefined method Illuminate\Database\Eloquent\Relations\BelongsTo::withTrashed()
Could not analyze class App\Order.
stale[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this issue is still present on the latest version of this library on supported Laravel versions, please let us know by replying to this issue so we can investigate further. Thank you for your contribution! Apologies for any delayed response on our side.

rogatty commented 4 years ago

The issue is still present on the latest version.

ssarljames commented 4 years ago

Any updates?

mfn commented 4 years ago

Add this PHPDoc before \Eloquent generated in _ide_helper.php file. Though I don't know if there is a good way if implementing this.

I did not look into this in detail but this suggestion make sense to me and AFAIK there's precedence. So, don't take my word for gold, but if it's about a) detecting the trait and b) just adding the static phpdoc method and slap a test on it, would this be a satisfying solution?

I think PRs are welcome 🤷‍♀️

stale[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this issue is still present on the latest version of this library on supported Laravel versions, please let us know by replying to this issue so we can investigate further. Thank you for your contribution! Apologies for any delayed response on our side.

miken32 commented 3 years ago

For getting the static methods to autocomplete on the model, like User::withTrashed()->get(), nothing is needed. The trait now includes the methods in its docblock header.

But using the methods on relationship methods like $user->posts()->withTrashed()->get() is still not possible. I've had partial success adding this to _ide_helper.php:

namespace Illuminate\Database\Eloquent\Relations {
    /**
     * @method \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder withTrashed()
     * @method \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder onlyTrashed()
     * @method \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder withoutTrashed()
     */
    class Relation {}
}

This stops the complaints about unknown methods, but it still won't autocomplete them (using PhpStorm.)

stale[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this issue is still present on the latest version of this library on supported Laravel versions, please let us know by replying to this issue so we can investigate further. Thank you for your contribution! Apologies for any delayed response on our side.

glorand commented 3 years ago

Any updates?

shehi commented 2 years ago

Any updates? @barryvdh isn't around?

barryvdh commented 2 years ago

Any pull requests?

Stalinko commented 2 years ago

5 years have passed, I think now I've grown enough to make a good PR, will try to do that in the nearest time 😊

RyanPaiva56 commented 2 years ago

I'm running into this issue on the latest version of everything. Is there something I should be enabling to resolve it?

Screenshot from 2021-12-30 12-48-51

shehi commented 2 years ago

Yea, same here. I can comfirm.