确定是否所有列都不为空,只有一列除外:Laravel



我有一个名为Applications的大表(30多列(。根据填写的列将确定申请是否完整。

我该如何在查询中说出以下内容?

如果除Application_Status外,所有列都不为Null将应用程序状态更新为True。

可能的长途:

$appCheck = Application::where('a, !=, null')
->where('b', '!=', null)
->where('c', '!=', null)
->where(etc... 30 more columns..., '!=', null)
->except(['m','n','application_status'])
)->first();
if($appCheck == true){
$appCheck->update([
'application_status' => 1,
])
}

有没有一种简单的方法可以做到这一点,这样我就不必写出每个列名了?即…

$appCheck = Applicaiton::whereNotNull('*')->except(['m','n','application_status'])->first()
if($appCheck == true){
$appCheck->update([
'application_status' => 1,
])
}

我的想法是将数据库表的所有列作为一个数组,然后通过删除不需要的列来过滤它们。最后,我们得到过滤后的列,并在"whereNotNull"方法中使用它们:

Application::whereNotNull(
array_values(
array_filter(
Schema::getColumnListing('applications'),
fn($column) => !in_array($column, ['m', 'n', 'application_status']),
),
),
)->first();

最新更新