繁体   English   中英

检查任何列中是否存在记录

[英]Check if record exists in ANY column

在我的 Laravel 应用程序中,我需要检查表中的 20 列是否有特定记录。 我已经搜索了这个答案,但只找到了一种方法来检查它是否存在于特定列中,但我需要检查所有列,我想知道是否有一种方法可以在没有循环的情况下做到这一点,例如:

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

假设$field->id是搜索词。 你可以试试

//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();

暂无
暂无

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

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