I'm trying to make this query work:
$deleteSubmissions = ViewedSubmission::where('user_id', Auth::user()->id)
->latest()
->skip(5)
->delete();
I want to delete all ViewedSubmissions records for the auth user EXCEPT for the latest 5. How can I make this work? Currently this doesn't delete anything, despite having more than 5 records.
I'd handle it this way:
$keep = ViewedSubmission::where('user_id', Auth::user()->id)
->latest()
->take(5)
->pluck('id');
ViewedSubmission::where('user_id', Auth::user()->id)
->whereNotIn('id', $keep)
->delete();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With