【问题标题】:Adding a exclusion to a query. Laravel 4向查询添加排除项。拉拉维尔 4
【发布时间】:2017-09-19 04:29:42
【问题描述】:

我正在尝试排除某个查询。

有4张桌子:

  • Published_books
  • Parameters_values
  • 发货
  • 订单

现在在第一个查询中,我询问具有列 (plaza_type) 且参数值为“best”的出版物

但是,我也想从第一个查询中排除:购买的出版物(第二个查询)。但是购买的出版物是在创建发货(表)和订单(表)时创建的,并且状态为“有效”、“部分”、“已发送”、“已接收”。

我有两个数组:

        $eliteBooks = BookPublication::join('parameter_values', 'published_books.plaza_type', '=', 'parameter_values.id')
    ->select('published_books.*')
    ->where(function($query) use($then, $now)
    {
        $query->whereBetween('published_books.created_at', [$then, $now])
              ->where('parameter_values.pvalue', '=', 'best');
    })
    ->orderBy('published_books.created_at', 'desc')->take(12)
    ->get()->chunk(6)->map(function($value)
    {
        return $value->chunk(3);
    });

第二个

    $recentBoughtExchanged = BookPublication::join('shipments', 'published_books.id', '=', 'shipments.published_book_id')
    ->join('orders', 'shipments.order_id', '=', 'orders.id')
    ->join('parameter_values', 'orders.order_status', '=', 'parameter_values.id')
    ->select('published_books.*')
    ->where(function($query) use($then, $now)
    {
        $query->whereBetween('orders.created_at', [$then, $now])
              ->where('parameter_values.pvalue', '=', 'valid')
              ->orWhere('parameter_values.pvalue', '=', 'partial')
              ->orWhere('parameter_values.pvalue', '=', 'sent')
              ->orWhere('parameter_values.pvalue', '=', 'received');
    })
    ->groupBy('published_books.id')
    ->orderBy('orders.created_at', 'desc')
    ->get()->chunk(6)->map(function($value)
    {
        return $value->chunk(3);
    });

有什么帮助吗?我想在第一个中排除第二个的结果,以便只显示可以购买的出版物。

【问题讨论】:

    标签: php mysql laravel-4


    【解决方案1】:

    解决方案在查询中添加filter

       $eliteBooks = BookPublication::join('parameter_values', 'published_books.plaza_type', '=', 'parameter_values.id')
        ->select('published_books.*')
        ->where(function($query) use($then, $now)
        {
            $query->whereBetween('published_books.created_at', [$then, $now])
                  ->where('parameter_values.pvalue', '=', 'best');           
        })
        ->orderBy('published_books.created_at', 'desc')->take(12)
        ->get()->filter(function($publication) {
            return $publication->can_be_bought;
        })->chunk(6)->map(function($value)
        {
            return $value->chunk(3);
        });
    

    $publication 定义在模型 BookPublication 中,can_be_bought 也定义在方法中。

    【讨论】:

      猜你喜欢
      • 2015-05-07
      • 1970-01-01
      • 1970-01-01
      • 2018-01-01
      • 1970-01-01
      • 2015-04-13
      • 2014-11-12
      • 2022-01-26
      • 2017-07-16
      相关资源
      最近更新 更多