Check if record exists in any column
P粉917406009
P粉917406009 2024-04-01 15:20:21
0
1
466

In my Laravel application, I need to check if there is a specific record in 20 columns in a table. I have searched for this answer but only found a way to check if it exists in a specific column but I need to check all columns and I was wondering if there is a way to do this without a loop, For example:

DB::table('cart')->where($fileId->id)->exists();

P粉917406009
P粉917406009

reply all(1)
P粉530519234

Assume $field->id is the search term. You can try

//use Illuminate\Support\Facades\Schema;

$columns = Schema::getColumnListing('cart');

$query = DB::table('cart');

$firstColumn = array_shift($columns);
$query->where($firstColumn, $field->id);

foreach($columns as $column) {
    $query->orWhere($column, $field->id);
}

$result = $query->exists();
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template