6

我试图了解如何将 SQL 查询转换为 laravel 中的查询构建器样式。

我的 SQL 查询是:

$tagid = Db::select("SELECT `id` FROM `wouter_blog_tags` WHERE `slug` = '".$this->param('slug')."'");

$blog = Db::select("SELECT * 
            FROM `wouter_blog_posts` 
            WHERE `published` IS NOT NULL 
            AND `published` = '1'
            AND `published_at` IS NOT NULL 
            AND `published_at` < NOW()
            AND (

            SELECT count( * ) 
            FROM `wouter_blog_tags` 
            INNER JOIN `wouter_blog_posts_tags` ON `wouter_blog_tags`.`id` = `wouter_blog_posts_tags`.`tags_id` 
            WHERE `wouter_blog_posts_tags`.`post_id` = `wouter_blog_posts`.`id` 
            AND `id` 
            IN (
             '".$tagid[0]->id."'
            )) >=1
            ORDER BY `published_at` DESC 
            LIMIT 10 
            OFFSET 0");

我现在最终转换为查询生成器的地方是:

   $test = Db::table('wouter_blog_posts')
->where('published', '=', 1)
->where('published', '=', 'IS NOT NULL')
->where('published_at', '=', 'IS NOT NULL')
->where('published_at', '<', 'NOW()')
  ->select(Db::raw('count(*) wouter_blog_tags'))
->join('wouter_blog_posts_tags', function($join)
{ 
$join->on('wouter_blog_tags.id', '=', 'wouter_blog_posts_tags.tags_id')
->on('wouter_blog_posts_tags.post_id', '=', 'wouter_blog_posts.id')
->whereIn('id', $tagid[0]->id);
})
->get();

我读过我不能在连接中使用 whereIn 。我现在得到的错误:

调用未定义的方法 Illuminate\Database\Query\JoinClause::whereIn()

我真的不知道如何将我的 SQL 转换为查询生成器。我希望当我看到我的查询的良好工作转换时,我能理解下一次我必须如何做。

4

2 回答 2

1

这对我有用:

DB::table('wouter_blog_posts') ->whereNotNull('published') ->where('published', 1) ->whereNotNull('published_at') ->whereRaw(' published_at< NOW()') ->whereRaw( "(SELECT count(*) FROM wouter_blog_tags INNER JOIN wouter_blog_posts_tagsON wouter_blog_tags. id= wouter_blog_posts_tags. tags_id WHERE wouter_blog_posts_tags. post_id= wouter_blog_posts. id AND id IN ( '".$tagid."' )) >=1") ->orderBy('published_at', 'desc') ->跳过(0)->采取(10)->分页($this->property('postsPerPage'));

于 2015-09-18T07:53:37.267 回答
0

The following Query Builder code will give you the exact SQL query you have within your DB::select:

DB::table('wouter_blog_posts')
    ->whereNotNull('published')
    ->where('published', 1)
    ->whereNotNull('published_at')
    ->whereRaw('`published_at` < NOW()')
    ->where(DB::raw('1'), '<=', function ($query) use ($tagid) {
        $query->from('wouter_blog_tags')
            ->select('count(*)')
            ->join('wouter_blog_posts_tags', 'wouter_blog_tags.id', '=', 'wouter_blog_posts_tags.tags_id')
            ->whereRaw('`wouter_blog_posts_tags`.`post_id` = `wouter_blog_posts`.`id`')
            ->whereIn('id', [$tagid[0]->id]);
    })
    ->orderBy('published_at', 'desc')
    ->skip(0)
    ->take(10)
    ->get();

The subquery condition had to be reversed because you can't have a subquery as the first parameter of the where method and still be able to bind the condition value. So it's 1 <= (subquery) which is equivalent to (subquery) >= 1. The query generated by the above code will look like this:

SELECT * 
FROM `wouter_blog_posts` 
WHERE `published` IS NOT NULL 
       AND `published` = 1 
       AND `published_at` IS NOT NULL 
       AND `published_at` < Now() 
       AND 1 <= (SELECT `count(*)` 
                 FROM `wouter_blog_tags` 
                      INNER JOIN `wouter_blog_posts_tags` 
                              ON `wouter_blog_tags`.`id` = 
                                 `wouter_blog_posts_tags`.`tags_id` 
                 WHERE `wouter_blog_posts_tags`.`post_id` = 
                       `wouter_blog_posts`.`id` 
                        AND `id` IN ( ? )) 
ORDER  BY `published_at` DESC 
LIMIT  10 offset 0 

My process when creating more complex queries is to first create them and try them out in a SQL environment to make sure they work as indended. Then I implement them step by step with the Query Builder, but instead of using get() at the end of the query, I use toSql() which will give me a string representation of the query that will be generated by the Query Builder, allowing me to compare that to my original query to make sure it's the same.

于 2015-09-12T13:07:43.100 回答