加入头桌与位置表与laravel雄辩

时间:2018-01-10 09:08:05

标签: laravel join eloquent

我有以下结构:

(Laravel版本5.4)

我有一个头表(系列)和一个相关位置(帖子)的表格。 positionstable有三个值,用于确定是否应显示帖子,甚至不显示。系列表也有这些信息。 这些专栏是:'发布' (布尔),' published_from' (datetime)和" published_to' (日期时间)。

要显示帖子,系列和帖子,必须发布(发布= 1),并且实际日期/时间必须比系列和帖子的published_from值更重要,或者是euqal,并且实际日期/时间也必须低于或等于published_to值。

为了确定是否应该显示位置,我写了一个雄辩的范围:

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语句是正确的,

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表中存在的每一列都具有系列表的值。

感谢您的帮助; - )

马库斯

1 个答案:

答案 0 :(得分:0)

我用whereHas()

解决了这个问题
    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;
    }