首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Join head table with position table with laravel

Join head table with position table with laravel
EN

Stack Overflow用户
提问于 2018-01-10 17:08:05
回答 1查看 53关注 0票数 1

我有以下结构:

(Laravel版本5.4)

我有一个头表(系列),以及一个与相关职位(职位)的表。posts表有三个值,它们决定了帖子是否应该显示,甚至不显示。系列表也包含此信息。这些列是:'published‘(布尔值)、'published_from’(日期时间)和'published_to‘(日期时间)。

要显示帖子、系列和帖子,必须发布(published = 1),并且实际日期/时间必须大于或等于系列和帖子的published_from值,并且实际日期/时间也必须小于或等于published_to值。

为了检查是否应该显示位置,我编写了一个雄辩的作用域:

代码语言:javascript
复制
public function scopePublishedRestriction($query)
        {
            $date = Carbon::today();
            $today = date('Y-m-d', $date->getTimestamp());

            $query->accessible()
                ->join('learning_series', function ($join) use ($today) {
                    $join->on($this->table . '.learning_serie_id', 'learning_series.id')
                        ->where('learning_series.published', 1)
                        ->whereDate('learning_series.published_from', '<=', $today)
                        ->where(function ($join) use ($today) {
                            $join->orWhere(function ($join) use ($today) {
                                $join->whereDate('learning_series.published_to', '>=', $today);
                            })
                                ->orWhere('learning_series.published_to', '0000-00-00 00:00:00');
                        });
                })
                ->where($this->table . '.published', 1)
                ->where($this->table . '.published_from', '<=', $today)
                ->where(function ($query) use ($today) {
                    $query->orWhere(function ($query) use ($today) {
                        $query->whereDate($this->table . '.published_to', '>=', $today);
                    })
                        ->orWhere($this->table . '.published_to', '0000-00-00 00:00:00');
                });

            return $query;
        }

创建的SQL语句是正确的,

代码语言:javascript
复制
select * from `brshop_docs_learning_posts` 
inner join `brshop_docs_learning_series` on `brshop_docs_learning_posts`.`learning_serie_id` = `brshop_docs_learning_series`.`id` 
and `brshop_docs_learning_series`.`published` = ? 
and date(`brshop_docs_learning_series`.`published_from`) <= ? 
and ((date(`brshop_docs_learning_series`.`published_to`) >= ?) or `brshop_docs_learning_series`.`published_to` = ?) 
    where `brshop_docs_learning_posts`.`visible` = ? 
    and `brshop_docs_learning_posts`.`active` = ? 
    and `brshop_docs_learning_posts`.`published` = ? 
    and `brshop_docs_learning_posts`.`published_from` <= ? 
    and ((date(`brshop_docs_learning_posts`.`published_to`) >= ?) or `brshop_docs_learning_posts`.`published_to` = ?) 
    and `brshop_docs_learning_posts`.`deleted_at` is null

我认为,集合的合并是错误的。例如,这个集合中的每个帖子都有该系列的ID。因此,存在于系列表和posts表中的每一列都具有系列表的值。

感谢您的帮助;-)

马库斯

EN

回答 1

Stack Overflow用户

发布于 2018-01-10 21:27:27

我用whereHas()解决了这个问题

代码语言:javascript
复制
    public function scopePublishedRestriction($query)
    {
        $date = Carbon::today();
        $today = date('Y-m-d', $date->getTimestamp());

        $query->accessible()
            ->whereHas('serie', function($query) use($today){
                $query->where('learning_series.published', 1)
                    ->whereDate('learning_series.published_from', '<=', $today)
                    ->where(function ($query) use ($today) {
                        $query->orWhere(function ($query) use ($today) {
                            $query->whereDate('learning_series.published_to', '>=', $today);
                        })
                            ->orWhere('learning_series.published_to', '0000-00-00 00:00:00');
                    });
            })
            ->where($this->table . '.published', 1)
            ->where($this->table . '.published_from', '<=', $today)
            ->where(function ($query) use ($today) {
                $query->orWhere(function ($query) use ($today) {
                    $query->whereDate($this->table . '.published_to', '>=', $today);
                })
                    ->orWhere($this->table . '.published_to', '0000-00-00 00:00:00');
            });

        return $query;
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48183760

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档