简体   繁体   中英

How to check in Laravel, if a table is completly empty, BEFORE do anything

I wanna quest if there is a method to check if one model table is empty, before doing anything. I have inside my seeder class a method to generate fake entries inside the database. But before i call this method, i wanna be sure that nothing inside my 'company' table. The thing is most people give example like: Company::where(...). I don't wanna use a where, because i don't wanna find anything specific inside the table. I only wanna return a null or a zero without error to handle the value and call after this my insert method for seeding the database. Thank you very much

you can do like that without any where condition

$count = \DB::table('company')->count();
if($count == 0) {
   //add fake data...
}else {
  //data already there no need to add fake data
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM