2017-07-15 34 views
0

我有一个应该运行如果数组不为空,但由于某些原因,它仍然会运行,即使它是空的代码的代码。这里是我有:PHP Laravel如果(!空())运行时,空

$fifteenDays = Carbon::today('America/Los_Angeles')->subDays(15); 
$invoiceSent = Invoice::whereDate('invoiceDue', '<=',$fifteenDays)->where('isPaid', 0)->where('isPaying', 0)->get(); 

if (!empty($invoiceSent)) { 
    //Run Code In here 
} 

无论$invoiceSent是否为空,这将运行。

+0

那么是什么_is_ $ invoiceSent? var_dump($ invoiceSent)在'!empty'检查之前应该告诉你实际上是否需要它。如果它是一个对象(如结果集),它不会被视为空的。 – MatsLindh

+0

$ invoiceSent中有什么? empty()只满足一定数量的空标准,所以如果它是一个空键的数组,它将不会计数。阅读这里的文档:http://php.net/manual/en/function.empty.php – JonLuca

回答

0

get()方法回退Collection这是一个Countable

这导致empty实际上是正确的,而count()将返回0.这是违反直觉的,但仍然是语言特定的功能。

+0

谢谢,我改变了看count是否大于0,它对我有用。 –