daugaard47
daugaard47

Reputation: 1868

Determine if all columns are not null Except one: Laravel

I have a large table called Applications (30+ columns). Based on the columns that are filled out will determine if the application is complete.

How would I say the following in a query?

If all columns Do Not = Null, Except Application_Status Update Application Status to True.

Possible Long Way:

$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,
])
}

Is there a Short Hand way to do this so I don't have to write out each column name? IE...

$appCheck = Applicaiton::whereNotNull('*')->except(['m','n','application_status'])->first()

if($appCheck == true){
$appCheck->update([
'application_status' => 1,
])
}

Upvotes: 1

Views: 628

Answers (1)

Mike
Mike

Reputation: 432

My idea is to get all the columns of your database table as an array, then filter them by removing the not wanted columns. Finally we get the filtered columns and we use them in the `whereNotNull' method:

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

Upvotes: 1

Related Questions