由于我对所有可能的条件使用if else子句,因此我正在做过滤器以显示数据,并且代码越来越长。有没有更好的方法来编写这些代码?
这是我在过滤器上使用的示例代码之一。
HTML代码:
<div class="well well-sm col-xs-12">
<div class="row">
<div class="col-sm-2 col-xs-12">
<div class="form-group form-group-sm">
<label>Date </label>
<select class="form-control month" name="month">
<option value="1"> January </option>
<option value="2"> February </option>
<option value="3"> March </option>
<option value="4"> April </option>
<option value="5"> May </option>
<option value="6"> June </option>
<option value="7"> July </option>
<option value="8"> August </option>
<option value="9"> September </option>
<option value="10"> October </option>
<option value="11"> November </option>
<option value="12"> December </option>
</select>
<select class="form-control year" name="year">
<option value="2019"> 2019 </option>
<option value="2018"> 2018 </option>
<option value="2017"> 2017 </option>
<option value="2016"> 2016 </option>
<option value="2015"> 2015 </option>
<option value="2014"> 2014 </option>
<option value="2013"> 2013 </option>
<option value="2012"> 2012 </option>
<option value="2011"> 2011 </option>
<option value="2010"> 2010 </option>
<option value="2009"> 2009 </option>
</select>
</div>
</div>
<div class="col-sm-2 col-xs-12">
<div class="form-group form-group-sm">
<label>Customer</label>
<select class="form-control customer" name="customer">
<option value="all">All</option>
@foreach($customer_options as $key => $data)
<option value="{{ $data->ref_code}}">{{$data -> ref_code}}-{{ $data->name_t }}</option>
@endforeach
</select>
</div>
</div>
<div class="col-sm-3 col-xs-12">
<div class="form-group form-group-sm">
<label>Search Text </label>
<input type="text" name="search" class="form-control" id="search">
<input type="submit" name="" value="Show Data" id="show" class="btn btn-success btn-sm">
</div>
</div>
</div>
</div>
控制器中的过滤器功能
public function filterQAPassed(Request $request)
{
$current_year = $request['year'];
$current_month = $request['month'];
$customer_options = Ref::where('ref_group', 'C')->orderBy('name_t', 'asc')->get();
$customer = $request['customer'];
$search = $request['search'];
if ($customer != 'all') {
$process_trans = DB::table('process_trans')
->leftJoin('jt_d', function ($join) {
$join->on('process_trans.doc_code', '=', 'jt_d.doc_code');
$join->on('process_trans.book', '=', DB::raw('concat(jt_d.book,"-",jt_d.seq)'));
})
->leftJoin('jt_h', function ($join) {
$join->on('jt_d.doc_code', '=', 'jt_h.doc_code');
$join->on('jt_d.book', '=', 'jt_h.book');
})
->leftJoin('astaff', 'process_trans.staff_code', '=', 'astaff.staff_code')
->select(
'process_trans.staff_code',
'process_trans.p_code',
'process_trans.book',
'process_trans.qty',
'jt_d.product_code',
'jt_d.name_t as product_name',
'jt_d.dwg_file',
'jt_d.part_no',
'jt_d.cost',
'jt_h.cc',
'jt_h.sale_name',
'jt_h.ref_code',
'process_trans.description',
'astaff.name_t as staff_name',
'process_trans.from_time as qa_date',
)
->whereMonth('process_trans.from_time', $current_month)
->whereYear('process_trans.from_time', $current_year)
->where('process_trans.p_code2', '=', 'Finished')
->where('ref_code',$customer)
->where(function ($query) use ($search) {
$query->where('jt_h.book', 'LIKE', '%' . $search . '%');
$query->orWhere('jt_h.ref_name', 'LIKE', '%' . $search . '%');
$query->orWhere('jt_h.reference', 'LIKE', '%' . $search . '%');
$query->orWhere('jt_d.product_code', 'LIKE', '%' . $search . '%');
$query->orWhere('process_trans.description', 'LIKE', '%' . $search . '%');
$query->orWhere('astaff.name_t', 'LIKE', '%' . $search . '%');
})
->orderBy('process_trans.from_time')
->get();
return view('production_system.qa_passed', compact('search', 'current_month', 'current_year', 'process_trans','customer_options','customer'));
}else if($customer =='all'){
$process_trans = DB::table('process_trans')
->leftJoin('jt_d', function ($join) {
$join->on('process_trans.doc_code', '=', 'jt_d.doc_code');
$join->on('process_trans.book', '=', DB::raw('concat(jt_d.book,"-",jt_d.seq)'));
})
->leftJoin('jt_h', function ($join) {
$join->on('jt_d.doc_code', '=', 'jt_h.doc_code');
$join->on('jt_d.book', '=', 'jt_h.book');
})
->leftJoin('astaff', 'process_trans.staff_code', '=', 'astaff.staff_code')
->select(
'process_trans.staff_code',
'process_trans.p_code',
'process_trans.book',
'process_trans.qty',
'jt_d.product_code',
'jt_d.name_t as product_name',
'jt_d.dwg_file',
'jt_d.part_no',
'jt_d.cost',
'jt_h.cc',
'jt_h.sale_name',
'jt_h.ref_code',
'process_trans.description',
'astaff.name_t as staff_name',
'process_trans.from_time as qa_date',
)
->whereMonth('process_trans.from_time', $current_month)
->whereYear('process_trans.from_time', $current_year)
->where('process_trans.p_code2', '=', 'Finished')
->where(function ($query) use ($search) {
$query->where('jt_h.book', 'LIKE', '%' . $search . '%');
$query->orWhere('jt_h.ref_name', 'LIKE', '%' . $search . '%');
$query->orWhere('jt_h.reference', 'LIKE', '%' . $search . '%');
$query->orWhere('jt_d.product_code', 'LIKE', '%' . $search . '%');
$query->orWhere('process_trans.description', 'LIKE', '%' . $search . '%');
$query->orWhere('astaff.name_t', 'LIKE', '%' . $search . '%');
})
->orderBy('process_trans.from_time')
->get();
return view('production_system.qa_passed', compact('search', 'current_month', 'current_year', 'process_trans','customer_options','customer'));
}
}
除了使用扩展的if-else子句以外,是否还有更好的方法用更清晰的代码进行过滤? 对于上面的示例,只有一个选项需要选择,即“客户”,它可以吸引所有客户或选择特定的客户。 对于我的其他表格,需要指定许多选项。除了条件以外,无需重复相同的事情,编写这些代码的另一种方法是什么?
答案 0 :(得分:2)
是的,您可以为此使用conditional clauses。
->when($customer === 'all', function ($query) {
// Special query parts for all customers
}, function ($query) {
// Optional query parts for when not all customers
})