How to come up with auto-Erasure table?

Guys, I have a table with fields
id
name
vid
comm
created_at
updated_at

In this table data have been entered, and then displays the last date you make a maximum of 500 records with the same values for the vid
It is logical that the records outside the sample of 500 is not useful.
How to make so that if entries with the same vid more than 500, then remove excess by date over the old one?
July 8th 19 at 16:39
2 answers
July 8th 19 at 16:41
Solution
Read docks... eventually the ideal option for Laravel without a bike will be:
$count = DB::table('tbl_name')->where('vid', '=', $vid)->count();
DB::table('tbl_name')->where('vid', '=', $vid)
 ->orderBy('created_at', 'asc')
->skip(500)->take($count-500)
 ->delete();
Probably take not needed here. You have already defined the offset - then, in predikaat get all the remaining entries. Moreover, You take in count the whole table, not just the $vid. - vito.Hoeg commented on July 8th 19 at 16:44
To the account count as a typo, added where. And on account of take is not clear... It is in Laravel skip work along with take, at first I also thought that without it will... - emilia_Treutel commented on July 8th 19 at 16:47
July 8th 19 at 16:43
There are many solutions, here is an example of one of them:

1) using the count and group by will get a list of vid - number, where the number more than 500
2) Pass on the array vid, vipolam the delete request with limit and sort by date.
Something in his head and come, but how to choose where the number over 500 ? - vito.Hoeg commented on July 8th 19 at 16:46
:
Model::whereRaw('count(*) > 500')->groupBy('vid')->get();
- emilia_Treutel commented on July 8th 19 at 16:49

Find more questions by tags PHPLaravelMySQLYii