Laravel-如何在DB Raw中通过下拉菜单进行过滤
我有这个模型课:Laravel-如何在DB Raw中通过下拉菜单进行过滤,laravel,Laravel,我有这个模型课: class Billings extends Model { protected $table = 'billings'; protected $fillable = [ 'msisdn', 'created_at', 'amount', 'billing_channel', ]; protected $guarded = [ 'id' ]; } 控制器 public function billingsReport(
class Billings extends Model
{
protected $table = 'billings';
protected $fillable = [
'msisdn',
'created_at',
'amount',
'billing_channel',
];
protected $guarded = [
'id'
];
}
控制器
public function billingsReport(Request $request)
{
$billings = DB::table('billings')
->select(
'msisdn',
DB::raw('created_at as created_date'),
'amount',
'billing_channel'
)
->orderByRaw('created_at DESC');
$render=[];
if(isset($request->msisdn))
{
$billings=$billings->where('msisdn','like','%'.$request->msisdn.'%');
$render['msisdn']=$request->msisdn;
}
if(isset($request->billing_channel))
{
$billings=$billings->where('billing_channel','like','%'.$request->billing_channel.'%');
$render['billing_channel']=$request->billing_channel;
}
$billings= $billings->orderBy('created_at','DESC');
$billings= $billings->paginate(15);
$billings= $billings->appends($render);
$data['billings'] = $billings;
return view('report.billingsReport',$data);
}
请注意,计费信道字段位于计费表中
看法
{{Form::model(request(),['method'=>'get']])}
{Form::text('msisdn',null,['class'=>'Form-control','placeholder'=>'msisdn'])}
{{Form::submit('Search',['class'=>'btn btn warning'])}
{{Form::close()}}
msisdn的文本筛选器已在运行
从我的控制器和视图中,我过滤msisdn,并在视图中使用form::text。现在的问题是,如何使用form::select
如何修改我的模型、视图和控制器来实现这一点?您的模型是正确的,但对于控制器,我建议您使用模型来获取数据,对于前端,您也可以使用
select2
来显示和填充,帮助您从select中筛选数据,它非常容易使用,对于控制器,您可以使用以下内容:
public function billingsReport(Request $request)
{
$msisdn = $request->msisdn;
$billings = Billings::select('msisdn', 'created_at as created_date', 'amount', 'billing_channel')
->orderBy('created_at', 'DESC')
->when(isset($request->msisdn), function($query) use ($msisdn) {
$query->where('msisdn','like','%'.$msisdn.'%');
})
->paginate(15);
return view('report.billingsReport', compact('billings'));
}
对于您使用的视图:
<select class="form-control" id="yourid">
@foreach($billings as $billing)
<option value="{{ your key }}">{{ the filed that you want to display }}</option>
@endforeach
</select>
@foreach($billings作为$billing)
{{要显示的字段}
@endforeach
获取一组可编辑的BillingChannel并将其传递给视图
然后您可以在表单中添加如下内容:
@foreach($billingChannel作为$billingChannel)
值}}>{{$billingChannel->name}
@endforeach
您的控制器中似乎已经有代码,因此这应该是您所需要的全部
如果您想使用Laravel Collective的
表单
外观,请查看他们的外观,了解如何使用它。Laravel 5.8version@JavierEmmanuelMercedes. 好像你不理解我。我已经为msisdn做了文本过滤器。现在我想为付费频道添加另一个过滤器,但它应该是一个下拉列表。我不知道如何将它添加到我的代码中。我还想坚持使用视图中使用的表单::
<select class="form-control" id="yourid">
@foreach($billings as $billing)
<option value="{{ your key }}">{{ the filed that you want to display }}</option>
@endforeach
</select>