【问题标题】:Filter Function in LaravelLaravel 中的过滤功能
【发布时间】:2018-09-24 12:07:51
【问题描述】:

你好,我建立了一个过滤系统 所以我有一个选择框,我可以在其中选择我的选项,然后单击一个按钮并进入另一个页面,在那里我可以看到我的所有结果。

这是我的控制器的一部分

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();

但是我现在有问题 我希望如果我的字段为空,则不应过滤

就是这样

 if(empty($request->get('typ'))){}
 else{->where('typ', $typ)}

但它不起作用我不知道如何使用 if/else 收集我的数据库条目? 有谁知道它是如何工作的?

谢谢!

我像这样改变了我的功能,但我得到了一个错误

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'));
    }

如果我把get放在这个之后

$angebots = DB::table('angebots') ->orderBy('stellenname', 'asc')->get();

我没有错误,但如果我把它放在如果它显示我这个错误之后:

未定义属性:Illuminate\Database\MySqlConnection::$firma(查看: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 一切正常

所以我有变量 $row,它显示我得到了多少结果,如果我过滤它显示正确的结果数量

但是如果 foreach 不起作用,我现在如何显示我的结果???

【问题讨论】:

    标签: laravel filter


    【解决方案1】:

    我使用这样的过滤器表单

    class UserFilterForm extends UserRepository
    {
        protected $fillable = [
            'id',
            'name',
            'email',
        ];
    
        public static function rules() :array
        {
            return [
                'id'        => 'nullable|integer|exists:users,id',
                'name'      => 'nullable|string',
                'email'     => 'nullable|email',
            ];
        }
    
        public function filter()
        {
            $qb = self::query()->with('roles');
    
            if ($this->id) {
                $qb->where('id', $this->id);
            }
    
            if ($this->name) {
                $qb->where('name', 'like', "%{$this->name}%");
            }
    
            if ($this->email) {
                $qb->where('email', 'like', "%{$this->email}%");
            }
    
            return $qb->paginate(10);
        }
    }
    

    以及我在控制器中所做的事情:

    class UserController {
        public function index(Request $request)
        {
            $form = new UserFilterForm;
    
            $form->fill($request->all());
    
            $this->validate($request->all(), $form::rules());
    
            return view('users', compact($form->filter()));
        }
    }
    

    【讨论】:

    • 是的,你的方法是对的。我编辑了答案:)
    【解决方案2】:

    你很接近。您需要等待-&gt;get() 直到ifs 之后。这是因为一旦你执行了get(),查询就会被执行,你不能再添加过滤器了。

    $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
    

    【讨论】:

    • 它有效!但也差不多,你能看看吗?谢谢!
    • 我编辑了我的答案。我忘记将get 的结果放回变量中。对于 angelbots,它应该是 $angebots = $angebots-&gt;get();
    猜你喜欢
    • 2012-03-26
    • 1970-01-01
    • 2014-11-01
    • 2010-11-23
    • 2013-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-10
    相关资源
    最近更新 更多