2

is there any way to retrieve posts that are related to a soft deleted user in laravel 5.4? Im thinking since the user is still in the database there should be a way.

  • 1
    Usually "soft deleting" is done by setting a Boolean Flag "Deleted" to true on that record. The Queries skip over the ones with "Deleted" for pure user side listings. But as you said, they are still in there for direct links or getting their name for existing posts. I have no experience with laravel 5.4, but there should be no technical reason for there being no way. Both an Override or a Default value could easily accomplish this. Would bet more on a override (performance reasons with DB queries). – Christopher May 07 '17 at 01:35
  • Soft-deleting a user a) leaves the user in the database (go look) and b) does nothing to its relationships (unless you coded your own logic to do that). Have you *tried* to access these posts in any way? What code are you using? – ceejayoz May 07 '17 at 01:54
  • yes but for some reason when i soft delete a user along with its post it fails everytime public function destroy($id) { $admins = User::findOrFail($id); $admins ->project()->delete(); $admins->project()->delete(); return redirect()->back(); } this is the destroy method im using and ive already set up the relations and everything else – kristi tanellari May 07 '17 at 01:58
  • Yes, of course that fails. The user can't be found, because it was deleted. You don't want normal queries to return deleted records. You need to use `withTrashed` to access deleted items, or you could delete the relationships before the user. This is very clearly laid out in [the documentation](https://laravel.com/docs/5.4/eloquent#soft-deleting) in the "Querying Soft Deleted Models" section. – ceejayoz May 07 '17 at 02:04

4 Answers4

6

When you soft delete a record, it gets flagged as deleted in the database, but the record is still there.

You can still retrieve it like this:

$users = App\User::withTrashed()->get();
Agu Dondo
  • 12,638
  • 7
  • 57
  • 68
1

I didn't prefer to restore a deleted post and keep the own user deleted cause that's will return a broken relation.

but if you want to restore it and know the id of the deleted user you can restore like that:

Post::onlyTrashed()->where('user_id', $id)->restore();

Again, I recommend to not doing that.

MohamedSabil83
  • 1,529
  • 7
  • 12
0

$onlySoftDeleted = Post::onlyTrashed()->get();

to get All deleted records

-1

This should work, I think.

Post::whereHas('users', function ($q) {
    $q->whereNotNull($q->getModel()->getQualifiedDeletedAtColumn());
})->get();

It's basically add a where clause to joining table deleted_at column which is not null. Or this, single line one perhaps works too.

User::with('posts')->onlyTrashed()->get()->map->posts->collapse();

onlyTrashed is a method defined in SoftDeletes trait as described here. There exists more 'scope extensions' beside onlyTrashed, which are restore, withTrashed, withoutTrashed.

I don't know, but I think adding ->unique('id') after collapse is good, too.

Chay22
  • 2,834
  • 2
  • 17
  • 25
  • This is waaaaaay overcomplicating things when OP just needs `User::withTrashed()->whereId($id)->first();` – ceejayoz May 07 '17 at 02:12
  • @ceejayoz OP question: is there any way to retrieve **posts** that are related to a soft deleted **user**. A relation. – Chay22 May 07 '17 at 02:13
  • ok but now whe i try to delete both user and post it just deletes the user and not the post. public function destroy($id) { $admins = User::findOrFail($id); $admins ->delete(); $admins->project()->delete(); return redirect()->back(); } with this code which is the code used on laracasts. for some reason it doesn't get the post associated to the used but just gets the user and deletes it.this is the error it shows FatalThrowableError in HomeController.php line 70: Call to a member function delete() on null – kristi tanellari May 07 '17 at 02:14
  • @Chay22 Once OP has the user, they just do `$user->posts` like normal. – ceejayoz May 07 '17 at 02:15
  • @ceejayoz As normal as second snippet doesn't it? – Chay22 May 07 '17 at 02:19
  • @user3089096 How about reverse the order, I mean, `$admins->project()->delete(); $admins ->delete();` – Chay22 May 07 '17 at 02:19
  • still no.somehow $admins->project()->delete() doesn't go through – kristi tanellari May 07 '17 at 02:22
  • Have you tried using event? http://stackoverflow.com/a/20108037/5816907 – Chay22 May 07 '17 at 02:45
  • still the same.doesn't get the post related – kristi tanellari May 07 '17 at 02:51