0
Post::whereReplyTo($request->input('reply_to'))
        ->orderBy('updated_at', 'desc')
        ->offset(Config::PAGE_SIZE * Config::MAX_PAGES)
        ->take(1024)
        ->delete();

I intend to fire this when post count reaches 4 in order to maintain a maximum of 4 posts, in this case.

Problem is it deletes ALL posts, not only those that I intended to delete

I'm becoming frustrated, why is this happening? theres no error, and I toSql'd the query and nothing is wrong, the selction part is correct, I tried it, so why is it deleting ALL posts???

DeadlyBacon
  • 531
  • 4
  • 17
  • 2
    What are the values of `$request->input('reply_to')`, `Config::PAGE_SIZE`, and `Config::MAX_PAGES` when you `dd()`/`var_dump()` them? Also what is the output of `Post::whereReplyTo($request->input('reply_to'))->orderBy('updated_at', 'desc')->offset(Config::PAGE_SIZE * Config::MAX_PAGES)->take(1024)->toSql()`? – Samsquanch Jun 27 '16 at 19:42
  • negative 1 in this case. but that's a valid value in my app, Page size is 2 and max pages is 2 – DeadlyBacon Jun 28 '16 at 12:38
  • hold on ill toSql it right now – DeadlyBacon Jun 28 '16 at 12:38
  • select * from `a2_posts` where `reply_to` = ? order by `updated_at` desc limit 1024 offset 4 – DeadlyBacon Jun 28 '16 at 12:41
  • I assume that ? means its parameterized and the replyto is added upon executing the query I guess? – DeadlyBacon Jun 28 '16 at 12:42
  • worst of all: I dd'd the result of replacing delete() with get() and they are correct. WHAT THE ACTUAL F**K – DeadlyBacon Jun 28 '16 at 13:05

2 Answers2

1

Well, if your research is correct, you should still be able to do it with two steps:

$delete_posts = Post::select('id')->whereReplyTo($request->input('reply_to'))
        ->orderBy('updated_at', 'desc')
        ->offset(Config::PAGE_SIZE * Config::MAX_PAGES)
        ->take(1024)
        ->get()->toArray();

Post::whereIn('id', $delete_posts)->delete();
Samsquanch
  • 8,866
  • 12
  • 50
  • 89
0

I think I'm on to something I appended a listener to log the last query after running my Delete the result was:

delete from `a2_posts` where `reply_to` = ? order by `updated_at` desc limit 1024

so where is my offset? I googled around and I think you cant use offset + delete.

retarded? yes, very much so.

This is why I like Mongo better.

It makes more sense.

Source: Mysql delete statement with limit

so what I think I'll do is query, then iterate, then delete.

SQL is pure genius sometimes I swear to God.

DeadlyBacon
  • 531
  • 4
  • 17