如果Laravel中的ID号为空,则不显示过滤器

问题描述 投票:0回答:1

我想做一个多重过滤器,其中一个带有一个ID号,但是在输入ID号之前没有数据出现。

 $postlocations = $postlocationsquerynew
                ->whereHas('postlog', function (Builder $querypost) use($request){
                    $log = $request->log;
                    $id = $request->id;
                    $querypost->where('id', $id)->where('log', 'like', '%' . $log . '%')->where('id', $id);
                })
                ->get();
php laravel
1个回答
0
投票

如果存在给定的when值,则使用$q->where('id', $id)->where('log', 'like', '%' . $log . '%')来应用$id语句:

$postlocations = $postlocationsquerynew
                ->whereHas('postlog', function (Builder $querypost) use($request){
                    $log = $request->log;
                    $id = $request->id;
                    $querypost->when($id, function($q, $id) use ($log) {
                           $q->where('id', $id)->where('log', 'like', '%' . $log . '%');
                    });
                })
                ->get();
© www.soinside.com 2019 - 2024. All rights reserved.