简体   繁体   English

如何检查 Laravel,如果表完全空了,在做任何事情之前

[英]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(...).大多数人给出的例子是:Company::where(...)。 I don't wanna use a where, because i don't wanna find anything specific inside the table.我不想使用 where,因为我不想在表格中找到任何特定的东西。 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.我只想返回一个 null 或一个没有错误的零来处理该值并在此之后调用我的插入方法来为数据库做种。 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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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