【发布时间】:2021-10-05 00:18:49
【问题描述】:
如果有值先演示一下:
文本字段:
<input type="text" id="search_category" class="search_category" placeholder="Search by Category" value="a">
Ajax 函数:(显示正在获取的文本字段中的数据)
var search_category = $('#search_category').val();
------------------------------------------
$.ajax({
type: "GET",
url: "/clinical/bbr-category-configuration-data/"+search_category,
SQL 选择:
public function fetchcategory($search_category){
$all_categories = HmsBbrCategory::query()
->when(empty($search_category), function ($query) {
$query->where('category_id', '>=', '1');
})
//iLike is case insensitive
->when(!empty($search_category), function ($query) use ($search_category) {
$query->where('category_name', 'iLIKE', "%$search_category%");
})
->orderBy('category_id', 'ASC')
->get(); }
由于我的文本字段有value="a",查询触发!empty(),显示:
我的问题是,如果我将文本字段更改为空白的 value="" 并触发 when(empty()),则行不显示,我收到错误
404 not found
"message": "",
"exception": "Symfony\\Component\\HttpKernel\\Exception\\NotFoundHttpException",
如果我的文本字段为空,如何阻止此错误的发生?
总结:如果搜索值为空,则尝试输出所有行,现在出现 404 错误
更新:试过了,如果文本字段为空,我仍然无法搜索
url: "/clinical/bbr-category-configuration-data/?search="+search_category,
Route::get('/bbr-category-configuration-data/?search={search_category}', [BBRCategoryConfigurationController::class,'fetchcategory']);
$search_category = request('search');
$all_categories = HmsBbrCategory::query()
->when(empty($search_category), function ($query) {
$query->where('category_id', '>=', 1);
})
//iLike is case insensitive
->when(!empty($search_category), function ($query) use ($search_category) {
$query->where('category_name', 'iLIKE', "%$search_category%");
})
->orderBy('category_id', 'ASC')
->get();
【问题讨论】:
标签: laravel search eloquent http-status-code-404 is-empty