[英]Check if record exists in ANY column
In my Laravel application, I need to check if any of my 20 columns in a table has a specific record.在我的 Laravel 应用程序中,我需要检查表中的 20 列是否有特定记录。 I've searched for this answer, but only found a way to check if it exists in a specific column, but i need to check in all columns, and i was wondering if there's a way to do that without a loop, something like:
我已经搜索了这个答案,但只找到了一种方法来检查它是否存在于特定列中,但我需要检查所有列,我想知道是否有一种方法可以在没有循环的情况下做到这一点,例如:
DB::table('cart')->where($fileId->id)->exists();
Assuming $field->id
is the search term.假设
$field->id
是搜索词。 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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.