根据 laravel 中的 select 下拉列表查询过滤器构建
Query building for filter based on select dropdown in laravel
我对Laravel系统有点陌生,所以请多多包涵。
目前我已将图像存储在数据库中,它们有列 featured
和 approved
。他们的状态均为 0
和 1
。 Approved/Not 批准。
我想要做的是能够从管理仪表板进行过滤:
- 所有图像(这将是所有图像,无论它们处于什么状态 - 0 和 1
- 仅批准 - 状态 1
- 未获批准 - 状态 0
这是我的 index.blade.php
<div class="col-md-2">
<div class="form-group">
<label for="featured">Featured</label>
<select class="form-control" name="featured" id="featured">
<option value=""> All</option>
<option value="1">Yes</option>
<option value="0">No</option>
</select>
</div>
</div>
<div class="form-group">
<label for="approved">Approved</label>
<select class="form-control" name="approved" id="approved">
<option value=""> All</option>
<option value="1">Yes</option>
<option value="0">No</option>
</select>
</div>
<button type="submit" class="btn btn-primary">Search</button>
这是我要在控制器中构建的查询
public function index(Request $request)
{
$approved = $request->approved;
$featured = $request->featured;
$images = Images::when($approved, function($query, $approved) {
if ($approved == '0') {
return $query->where('approved', 0);
} else if ($approved == '1' ){
return $query->where('approved', 1);
} else {
return $query->where('approved', [0,1]);
}
})
->when($featured, function($query, $featured) {
if ($featured == '0') {
return $query->where('featured', 0);
} else if ($featured == '1' ){
return $query->where('featured', 1);
} else {
return $query->where('featured', [0,1]);
}
})
->latest()->get();
return view( .... );
}
这是我在路线中的内容
Route::resource('images', 'ImagesController')->except('show');
第一个问题是,当我选择选项“全部”时它不显示任何内容,而当我选择“否”或“是”时它显示相同的结果。
我也不认为这是构建此类查询的正确方法。
试试这个代码跳转,它会对你有所帮助
<form action="{{route('images.index')}}">
<div class="col-md-2">
<div class="form-group">
<label for="featured">Featured</label>
<select class="form-control" name="featured" id="featured">
<option {{is_null(request()->input('featured')) ? 'selected' : ''}} value=""> All</option>
<option {{request()->input('featured') == 1 ? 'selected' : ''}} value="1">Yes</option>
<option {{!is_null(request()->input('featured')) && request()->input('featured') == 0 ? 'selected' : ''}} value="0">No</option>
</select>
</div>
</div>
<div class="form-group">
<label for="approved">Approved</label>
<select class="form-control" name="approved" id="approved">
<option {{is_null(request()->input('approved')) ? 'selected' : ''}} value=""> All</option>
<option {{request()->input('approved') == 1 ? 'selected' : ''}} value="1">Yes</option>
<option {{!is_null(request()->input('approved')) && request()->input('approved') == 0 ? 'selected' : ''}} value="0">No</option>
</select>
</div>
<button type="submit" class="btn btn-primary">Search</button>
</form>
public function index(Request $request)
{
$approved = $request->approved;
$featured = $request->featured;
$images = Images::when(!is_null($approved), function ($query) use ($approved) {
return $query->where('approved', $approved);
})->when(!is_null($featured), function ($query) use ($featured) {
return $query->where('featured', $featured);
})->latest()->get();
return view( .... );
}
我对Laravel系统有点陌生,所以请多多包涵。
目前我已将图像存储在数据库中,它们有列 featured
和 approved
。他们的状态均为 0
和 1
。 Approved/Not 批准。
我想要做的是能够从管理仪表板进行过滤:
- 所有图像(这将是所有图像,无论它们处于什么状态 - 0 和 1
- 仅批准 - 状态 1
- 未获批准 - 状态 0
这是我的 index.blade.php
<div class="col-md-2">
<div class="form-group">
<label for="featured">Featured</label>
<select class="form-control" name="featured" id="featured">
<option value=""> All</option>
<option value="1">Yes</option>
<option value="0">No</option>
</select>
</div>
</div>
<div class="form-group">
<label for="approved">Approved</label>
<select class="form-control" name="approved" id="approved">
<option value=""> All</option>
<option value="1">Yes</option>
<option value="0">No</option>
</select>
</div>
<button type="submit" class="btn btn-primary">Search</button>
这是我要在控制器中构建的查询
public function index(Request $request)
{
$approved = $request->approved;
$featured = $request->featured;
$images = Images::when($approved, function($query, $approved) {
if ($approved == '0') {
return $query->where('approved', 0);
} else if ($approved == '1' ){
return $query->where('approved', 1);
} else {
return $query->where('approved', [0,1]);
}
})
->when($featured, function($query, $featured) {
if ($featured == '0') {
return $query->where('featured', 0);
} else if ($featured == '1' ){
return $query->where('featured', 1);
} else {
return $query->where('featured', [0,1]);
}
})
->latest()->get();
return view( .... );
}
这是我在路线中的内容
Route::resource('images', 'ImagesController')->except('show');
第一个问题是,当我选择选项“全部”时它不显示任何内容,而当我选择“否”或“是”时它显示相同的结果。
我也不认为这是构建此类查询的正确方法。
试试这个代码跳转,它会对你有所帮助
<form action="{{route('images.index')}}">
<div class="col-md-2">
<div class="form-group">
<label for="featured">Featured</label>
<select class="form-control" name="featured" id="featured">
<option {{is_null(request()->input('featured')) ? 'selected' : ''}} value=""> All</option>
<option {{request()->input('featured') == 1 ? 'selected' : ''}} value="1">Yes</option>
<option {{!is_null(request()->input('featured')) && request()->input('featured') == 0 ? 'selected' : ''}} value="0">No</option>
</select>
</div>
</div>
<div class="form-group">
<label for="approved">Approved</label>
<select class="form-control" name="approved" id="approved">
<option {{is_null(request()->input('approved')) ? 'selected' : ''}} value=""> All</option>
<option {{request()->input('approved') == 1 ? 'selected' : ''}} value="1">Yes</option>
<option {{!is_null(request()->input('approved')) && request()->input('approved') == 0 ? 'selected' : ''}} value="0">No</option>
</select>
</div>
<button type="submit" class="btn btn-primary">Search</button>
</form>
public function index(Request $request)
{
$approved = $request->approved;
$featured = $request->featured;
$images = Images::when(!is_null($approved), function ($query) use ($approved) {
return $query->where('approved', $approved);
})->when(!is_null($featured), function ($query) use ($featured) {
return $query->where('featured', $featured);
})->latest()->get();
return view( .... );
}