复杂查询:使用select,orderby和where子句基于laravel5.4中的过滤器?

时间:2017-06-06 07:35:06

标签: search filter laravel-5.4 laravel-query-builder

我正在为过滤搜索编写代码,其中用户从3个复选框的列表中选择至少一个选项,然后我必须为所选选项(复选框)编写查询。如果我尝试运行我的代码,那么我就会得到' RECURSION'错误如下:

Illuminate\Database\Query\Builder Object ( [connection] => Illuminate\Database\MySqlConnection Object ( [pdo:protected] => PDO Object ( ) [readPdo:protected] => [database:protected] => membrain [tablePrefix:protected] => [config:protected] => Array ( [driver] => mysql [host] => 127.0.0.1 [port] => 3306 [database] => membrain [username] => root [password] => [unix_socket] => [charset] => utf8mb4 [collation] => utf8mb4_unicode_ci [prefix] => [strict] => 1 [engine] => [name] => mysql ) [reconnector:protected] => Closure Object ( [this] => Illuminate\Database\DatabaseManager Object ( [app:protected] => Illuminate\Foundation\Application Object ( [basePath:protected] => C:\xampp\htdocs\membrainmain [hasBeenBootstrapped:protected] => 1 [booted:protected] => 1 [bootingCallbacks:protected] => Array ( ) [bootedCallbacks:protected] => Array ( [0] => Closure Object ( [this] => App\Providers\RouteServiceProvider Object ( [namespace:protected] => App\Http\Controllers [app:protected] => Illuminate\Foundation\Application Object *RECURSION* [defer:protected] => ) ) ) [terminatingCallbacks:protected] => Array ( )

我的表格是:

<form id="statistics" method="post" action="{{ action('MyController@store') }}"">
 <div class="form-group">
  <label><input type="checkbox" value="suppliers" name="suppliers">suppliers</label>
 </div>
 <div class="form-group">
  <label><input type="checkbox" value="campaign" name="campaign">campaign</label>
 </div>
 <div class="form-group">
  <label><input type="checkbox" value="clients" name="clients">clients</label>
 </div>
 <div class="form-group">
   <label for="start_date">From:</label><input type="text" name="startdate" id="startdate">
 </div>
 <div class="form-group">
   <label for="start_date">From:</label><input type="text" name="enddate" id="enddate">
 </div>
 <div class="form-group">
  <button type="submit" id="process" class="btn btn-primary submit">Run Report</button>
 </div>
</form>

我的查询如下:

if($request->suppliers == 'suppliers'){
            $results->leftjoin('suppliers AS s', 's.id', '=', 'l.supplier_id')
                    ->select('s.name AS Supplier')
                    ->groupBy('s.name')
                    ->orderBy('s.name');
        }
if($request->campaign == 'campaign'){
            $results->leftjoin('campaigns AS c', 'c.id', '=', 'l.campaign_id')
                    ->select('c.name AS Campaign')
                    ->groupBy('c.name')
                    ->orderBy('c.name');
        }
if($request->clients == 'clients'){
            $results->leftjoin('clients AS cl', 'cl.id', '=', 'l.client_id')
                    ->groupBy('cl.name')
                    ->orderBy('cl.name');
        }
$results->whereBetween('l.received', [$start, $end])
                ->orderBy('l.disposition')
                ->get()->toArray();
            $count = count($results);

我需要查询如下:

$results = DB::table('lead_audit AS l')
             ->leftjoin('suppliers AS s', 's.id', '=', 'l.supplier_id')
             ->leftjoin('campaigns AS c', 'c.id', '=', 'l.campaign_id')
             ->select('s.name AS Supplier', 'c.name AS Campaign', 'l.disposition AS Disposition')
             ->whereBetween('l.received', [$start, $end])
             ->groupBy('s.name', 'c.name', 'l.disposition')
             ->orderBy('s.name')
             ->orderBy('c.name')
             ->orderBy('l.disposition')
             ->get()->toArray();
$count = count($results);

更新:如果我打印查询,则显示如下

Array ( [query] => select `s`.`name` as `Supplier` from `lead_audit` as `l` left join `suppliers` as `s` on `s`.`id` = `l`.`supplier_id` where `l`.`received` between ? and ? group by `s`.`name` order by `s`.`name` asc, `l`.`disposition` asc [bindings] => Array ( [0] => 2017-06-01 [1] => 2017-06-06 ) [time] => 1 )

普通查询工作正常,但如果我根据过滤器编写,那么它无法正常工作。我认为查询订单丢失了,任何人都可以帮我解决这个问题。感谢。

0 个答案:

没有答案