Laravel 5 中的查询条件

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

我想根据查询结果显示每个省份的数据条数。

这是我使用的查询。但通过这样的代码,我得到的输出只是包含条件的每个省的数据数量

['t_entry.etitle', 'ilike', $title]

同时,条件

['t_entry.edesc','ilike',$title]
不被执行。所以我得到的数据量很小。怎么处理?

    public function getTotalEntryByTitle($title){
    $total = [];

    $province = t_data_enum::where('ekey', 'province')->orderBy('etext', 'ASC')->get();
    foreach($province as $a){
        $entrys = t_e_elem::selectRaw('t_entry.*,t_e_elem.*')
            ->join('t_entry', 't_e_elem.eid', '=', 't_entry.eid')
            ->join('t_e_value', 't_e_elem.fid', '=', 't_e_value.elid')
            ->join('t_entry_form', 't_e_value.fid', '=', 't_entry_form.fid')
            ->where([['t_e_elem.fuse', '=', 1], ['t_entry.etitle', 'ilike', $title], ['t_entry.edesc','ilike',$title]]);
        $entrys = $entrys->where('t_entry.estatus', '1');

        $formIdP = t_entry_form::where([['etype', 1], ['fname', 'field_province']])->first()->fid;
        $entrys = $entrys->where([['t_e_value.fid', '=', $formIdP], ['t_e_value.vvalue', '=', $a->eval],
            ['t_e_elem.fuse', '=', 1]]);

        $entrys = $entrys->distinct("t_entry.eid")
            ->where([['t_entry.etitle','ilike',$title], ['t_entry.edesc','ilike',$title]])
            ->get();

        array_push($total, [
            'name' => $a->etext,
            'count' => count($entrys)
        ]);
    }

    return $total;
}
php sql laravel postgresql
1个回答
0
投票

我将解释查询构建器如何在您的案例中构建查询:

以此声明为例:

$entrys = t_e_elem::where([['t_e_elem.fuse', '=', 1], ['t_entry.etitle', 'ilike', $title], ['t_entry.edesc','ilike',$title]]);

这将生成sql:

select * from `t_e_elems` where (`t_e_elem`.`fuse` = ? and `t_entry`.`etitle` ilike ? and `t_entry`.`edesc` ilike ?)

注意

and
,这是问题所在,您通常不希望
and
出现在您需要的搜索中
or
,所以,
sql
声明应该是:

select * from `users` where `t_e_elem`.`fuse` = ? and (`t_entry`.`etitle` ilike ? or `t_entry`.`edesc` ilike ?)

Laravel 查询构建器将是:

 t_e_elem::where('t_e_elem.fuse', '=', 1)
            ->where(function ($query) use ($title) {
                $query->where('t_entry.etitle', 'ilike', $title)
                    ->orwhere( 't_entry.edesc','ilike',$title);
            })

最终结果如下:

 $entrys = t_e_elem::selectRaw('t_entry.*,t_e_elem.*')
                ->join('t_entry', 't_e_elem.eid', '=', 't_entry.eid')
                ->join('t_e_value', 't_e_elem.fid', '=', 't_e_value.elid')
                ->join('t_entry_form', 't_e_value.fid', '=', 't_entry_form.fid')
                ->where('t_e_elem.fuse', '=', 1)
                ->where(function ($query) use ($title) {
                    $query->where('t_entry.etitle', 'ilike', $title)->orWhere('t_entry.edesc', 'ilike', $title);
                });
            $entrys = $entrys->where('t_entry.estatus', '1');

            $formIdP = t_entry_form::where([['etype', 1], ['fname', 'field_province']])->first()->fid;
            $entrys = $entrys->where([['t_e_value.fid', '=', $formIdP], ['t_e_value.vvalue', '=', $a->eval],
                ['t_e_elem.fuse', '=', 1]]);

            $entrys = $entrys->distinct("t_entry.eid")
                ->where(function($query) use ($title) {
                $query->where('t_entry.etitle','ilike', $title)->orWhere('t_entry.edesc', 'ilike', $title);})
                ->get();
© www.soinside.com 2019 - 2024. All rights reserved.