Php 带分页的Laravel困难联合查询

Php 带分页的Laravel困难联合查询,php,sql,laravel,eloquent,union,Php,Sql,Laravel,Eloquent,Union,我有这些表,可能在mysql或postgres数据库中: |===============================================================================| | articles | |========================================================

我有这些表,可能在mysql或postgres数据库中:

|===============================================================================| | articles | |===============================================================================| |id|user_id|state| title |text|created_at|updated_at|abstract|image| |==|=======|=====|====================|====|==========|==========|========|=====| |1 | 1 | 5 |L'hopital rule |... |2014-12-05| | ... | ... | |2 | 2 | 5 |Gaussian quadrature |... |2012-11-13|2014-12-05| ... | ... | |3 | 2 | 5 |Gauss elimination |... |2013-07-18|2014-03-09| ... | ... | |4 | 3 | 5 |Plant cell structure|... |2013-09-10|2013-12-15| ... | ... | |5 | 4 | 5 |Game theory |... |2011-02-15|2012-11-30| ... | ... | | . . . | |===============================================================================| |===========| |====================| |===========| | tag_groups| | tags | | users | |===========| |====================| |===========| |id| name | |id|id_article|id_tag| |id| name | |==|========| |==|==========|======| |==|========| |1 |Math | |1 | 1 | 1 | |1 |Tom Ther| |2 |Calculus| |2 | 1 | 2 | |2 |May Nick| |3 |Gradient| |3 | 2 | 1 | |3 |Math Ria| |4 |Geometry| |4 | 3 | 1 | |4 |Gauss Li| | . . . | | . . . | | . . . | |===========| |====================| |===========|
function like($string, $escape_char = '\\') {
    return str_replace(
        array($escape_char, '_', '%'),
        array($escape_char.$escape_char, $escape_char.'_', $escape_char.'%'),
        $string
    );
}

function wild($string) {                                                
    return DB::connection()->getPdo()->quote('%'.like($string).'%');    
}                                                                       

function build_sql($quoted_query) {                                     
    return "                                                            
        SELECT DISTINCT                                                 
          articles.id, articles.user_id, articles.state, articles.title,
          articles.image, articles.abstract, articles.text, articles.created_at,
          articles.updated_at                                           
        FROM (                                                          
          SELECT articles.*, ord                                        
          FROM (                                                        
            SELECT articles.*, 1 ord                                    
            FROM articles                                               
            LEFT JOIN tags ON tags.id_article = articles.id             
            LEFT JOIN tag_groups ON tags.id_tag = tag_groups.id         
            WHERE articles.state = 5 AND tag_groups.name ILIKE $quoted_query
            UNION                                                       

            SELECT articles.*, 2 ord                                    
            FROM articles                                               
            WHERE articles.state = 5 AND articles.title ILIKE $quoted_query
            UNION                                                       

            SELECT articles.*, 3 ord                                    
            FROM articles                                               
            LEFT JOIN users ON users.id = articles.user_id              
            WHERE articles.state = 5 AND users.name ILIKE $quoted_query 
            UNION                                                       

            SELECT articles.*, 4 ord                                    
            FROM articles                                               
            WHERE articles.state = 5 AND articles.text ILIKE $quoted_query
          ) articles                                                    
          ORDER BY ord ASC, articles.updated_at DESC                    
        ) articles;                                                     
    "                                                                       
}

$articles = DB::table((DB::raw($build_sql(wild($_query)))))->distinct();

$maxPages = ceil(count($articles->get()) / 9);                          
return View::make('index', array(                                       
            'articles' => $articles->simplePaginate(9),
            'maxPages' => $maxPages,                                    
));                                                                     
这不管用,我不知道该怎么办。有没有一种方法可以用雄辩的模型写出漂亮的文章?或者,如果没有,我应该如何更新build\u sql字符串?

是否对$quoted\u查询进行转义和引用?wild$\u查询将处理该问题,并且当我打印build\u sql的结果时,它将在那里转义和引用。 |===============================================================================| | articles | |===============================================================================| |id|user_id|state| title |text|created_at|updated_at|abstract|image| |==|=======|=====|====================|====|==========|==========|========|=====| |1 | 1 | 5 |L'hopital rule |... |2014-12-05| | ... | ... | |2 | 2 | 5 |Gaussian quadrature |... |2012-11-13|2014-12-05| ... | ... | |3 | 2 | 5 |Gauss elimination |... |2013-07-18|2014-03-09| ... | ... | |4 | 3 | 5 |Plant cell structure|... |2013-09-10|2013-12-15| ... | ... | |5 | 4 | 5 |Game theory |... |2011-02-15|2012-11-30| ... | ... | | . . . | |===============================================================================| |===========| |====================| |===========| | tag_groups| | tags | | users | |===========| |====================| |===========| |id| name | |id|id_article|id_tag| |id| name | |==|========| |==|==========|======| |==|========| |1 |Math | |1 | 1 | 1 | |1 |Tom Ther| |2 |Calculus| |2 | 1 | 2 | |2 |May Nick| |3 |Gradient| |3 | 2 | 1 | |3 |Math Ria| |4 |Geometry| |4 | 3 | 1 | |4 |Gauss Li| | . . . | | . . . | | . . . | |===========| |====================| |===========|
function like($string, $escape_char = '\\') {
    return str_replace(
        array($escape_char, '_', '%'),
        array($escape_char.$escape_char, $escape_char.'_', $escape_char.'%'),
        $string
    );
}

function wild($string) {                                                
    return DB::connection()->getPdo()->quote('%'.like($string).'%');    
}                                                                       

function build_sql($quoted_query) {                                     
    return "                                                            
        SELECT DISTINCT                                                 
          articles.id, articles.user_id, articles.state, articles.title,
          articles.image, articles.abstract, articles.text, articles.created_at,
          articles.updated_at                                           
        FROM (                                                          
          SELECT articles.*, ord                                        
          FROM (                                                        
            SELECT articles.*, 1 ord                                    
            FROM articles                                               
            LEFT JOIN tags ON tags.id_article = articles.id             
            LEFT JOIN tag_groups ON tags.id_tag = tag_groups.id         
            WHERE articles.state = 5 AND tag_groups.name ILIKE $quoted_query
            UNION                                                       

            SELECT articles.*, 2 ord                                    
            FROM articles                                               
            WHERE articles.state = 5 AND articles.title ILIKE $quoted_query
            UNION                                                       

            SELECT articles.*, 3 ord                                    
            FROM articles                                               
            LEFT JOIN users ON users.id = articles.user_id              
            WHERE articles.state = 5 AND users.name ILIKE $quoted_query 
            UNION                                                       

            SELECT articles.*, 4 ord                                    
            FROM articles                                               
            WHERE articles.state = 5 AND articles.text ILIKE $quoted_query
          ) articles                                                    
          ORDER BY ord ASC, articles.updated_at DESC                    
        ) articles;                                                     
    "                                                                       
}

$articles = DB::table((DB::raw($build_sql(wild($_query)))))->distinct();

$maxPages = ceil(count($articles->get()) / 9);                          
return View::make('index', array(                                       
            'articles' => $articles->simplePaginate(9),
            'maxPages' => $maxPages,                                    
));