问题描述
你好,我建立了一个过滤系统因此,我有一个选择框,可以在其中选择我的选项,然后单击一个按钮,然后转到另一个页面,我可以在其中看到所有结果.
Hello i build a filter systemso i have a select box where i can select my options and then click a button and come to another page where i can see all my results.
这是我控制器的一部分
public function getValues(Request $request){
$typ=$request->get('typ');
$stellentyp=$request->get('stellentyp');
$bereich=$request->get('bereich');
$abschluss=$request->get('abschluss');
$view = 'user.'.$stellentyp;
$row = DB::table('angebots')
->where('stellentyp', $stellentyp)
->count();
$fwds = DB::table('angebots')
->where('stellentyp', 'Freiwilligendienst')
->where('typ', $typ)
->where('bereich', $bereich)
->where('abschluss', $abschluss)
->orderBy('stellenname', 'asc')
->get();
$jobs = DB::table('angebots')
->where('stellentyp', 'Job')
->where('typ', $typ)
->where('abschluss', $abschluss)
->where('bereich', $bereich)
->orderBy('stellenname', 'asc')
->get();
但是我现在有一个问题我希望如果我的字段为空,则不应该过滤
but i have a problem nowi want that if my field is empty it should not filter
像这样
if(empty($request->get('typ'))){}
else{->where('typ', $typ)}
但是它不起作用我不知道如何使用if/else来收集我的数据库条目?有谁知道它如何工作?
but it doesnt work i dont know how i can collect my database entries with a if/else?Does anyone know how it could work?
谢谢!
我这样更改了我的功能,但出现错误
i changed my function like this but i get a eerror
public function getValues(Request $request){
$typ=$request->get('typ');
$stellentyp=$request->get('stellentyp');
$bereich=$request->get('bereich');
$abschluss=$request->get('abschluss');
$user = DB::table('users')->get();
$angebots = DB::table('angebots') ->orderBy('stellenname', 'asc');
if(!empty($request->get('stellentyp'))){
$angebots->where('stellentyp', $stellentyp);
}
$angebots->get();
$row = $angebots->count();
return view('user/angebots', compact('typ', 'stellentyp', 'bereich', 'abschluss', 'row', 'angebots', 'user'));
}
如果我把得到的东西放在这样的地方
if i put the get after this so like that
$angebots = DB::table('angebots') ->orderBy('stellenname', 'asc')->get();
我没有错误,但是如果我把它放在如果我看到此错误之后:
i have no error but if i put it after the if it shows me this error:
未定义的属性:Illuminate \ Database \ MySqlConnection :: $ firma(视图:C:\ wamp \ sites \ j4ylara \ resources \ views \ user \ angebots.blade.php)
Undefined property: Illuminate\Database\MySqlConnection::$firma (View: C:\wamp\sites\j4ylara\resources\views\user\angebots.blade.php)
这是我的观点:
{{$row}} Angebote insgesamt
<div class="row">
@foreach ($angebots as $angebot)
<div class="col-12 col-md-6 col-lg-4 pt-4">
<div class="card offer-card">
<div class="card-image">
<img class="img-fluid" src="{{ asset('uploads/avatars/' . $user[$angebot->firma -1]->avatar) }}">
</div>
<div class="card-body">
<h4 class="text-j4y-dark praktikumstitel">{{ $angebot->stellenname }}</h4>
<a href="{{ route('angebot.details',['id'=>$angebot->firma]) }}">Jetzt mehr anzeigen »</a>
</div>
</div>
</div>
@endforeach
</div>
如果我注释掉$ angebot变量,一切正常
if i comment out the variable $angebot everything works
所以我有变量$ row,它显示了我得到多少结果,如果我对其进行过滤,它会显示正确数量的结果
so i have the variable $row and it shows me how many results i get and if i filter it shows me the right number of results
但是如果foreach不起作用,我现在如何显示我的结果?
but how can i display my results now if the foreach doesnt work???
推荐答案
您很亲密.您需要等待->get()
,直到ifs
之后.之所以这样,是因为一旦执行get()
,查询就会被执行,并且您无法再添加过滤器.
You are close. You need to wait with the ->get()
until after the ifs
. This is so because once you execute the get()
the query is executed and you cannot add anymore filters.
$fwds = DB::table('angebots')
->where('stellentyp', $stellentyp)
->orderBy('stellenname', 'asc'); //The get is not needed yet
if ($this->typ) {
$fwds->where('typ', 'like', "%{$this->typ}%");
}
if ($this->bereich) {
$fwds->where('bereich', 'like', "%{$this->bereich}%");
}
if ($this->abschluss) {
$fwds->where('abschluss', 'like', "%{$this->abschluss}%");
}
$fwds = $fwds->get(); //Place it after the ifs
这篇关于Laravel中的过滤器功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!