You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+62Lines changed: 62 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -874,12 +874,74 @@ DB::table('table_name')
874
874
875
875
The `QueryBuilder::delete` method may be used to delete records from the table.
876
876
877
+
Unlike WordPress's `$wpdb->delete()` method, this implementation generates and executes a DELETE SQL statement directly, which allows for advanced features like ORDER BY and LIMIT.
878
+
879
+
#### Basic delete with WHERE
880
+
877
881
```php
878
882
DB::table('posts')
879
883
->where('post_author', 1)
880
884
->delete();
881
885
```
882
886
887
+
#### Delete with LIMIT
888
+
889
+
Limit the number of rows to delete:
890
+
891
+
```php
892
+
// Delete only the first 10 draft posts
893
+
DB::table('posts')
894
+
->where('post_status', 'draft')
895
+
->limit(10)
896
+
->delete();
897
+
```
898
+
899
+
#### Delete with ORDER BY and LIMIT
900
+
901
+
Control which rows are deleted when using LIMIT:
902
+
903
+
```php
904
+
// Delete the 100 oldest posts in trash
905
+
DB::table('posts')
906
+
->where('post_status', 'trash')
907
+
->orderBy('post_date', 'ASC')
908
+
->limit(100)
909
+
->delete();
910
+
```
911
+
912
+
#### Delete with LIKE patterns
913
+
914
+
Use pattern matching to delete rows:
915
+
916
+
```php
917
+
// Delete all posts with titles starting with "Draft:"
918
+
DB::table('posts')
919
+
->whereLike('post_title', 'Draft:%')
920
+
->delete();
921
+
```
922
+
923
+
#### Delete with complex WHERE conditions
924
+
925
+
Combine multiple WHERE clauses for precise deletion:
926
+
927
+
```php
928
+
// Delete auto-draft pages with IDs between 1 and 1000
929
+
DB::table('posts')
930
+
->where('post_type', 'page')
931
+
->where('post_status', 'auto-draft')
932
+
->whereBetween('ID', 1, 1000)
933
+
->delete();
934
+
935
+
// Delete posts using whereIn
936
+
DB::table('posts')
937
+
->whereIn('ID', [5, 10, 15, 20])
938
+
->delete();
939
+
```
940
+
941
+
**Important restrictions:**
942
+
- Table aliases in the FROM clause may not be supported on older database versions (MySQL < 8.0.24, MariaDB < 11.6). Avoid using table aliases when calling `delete()`.
943
+
- JOINs are not supported in DELETE statements with this implementation.
0 commit comments