连接方法中的Laravel范围不起作用

时间:2018-03-21 04:34:24

标签: php laravel scope

这是我的功能:

public function getspecifications($id) {

    $specifications = DB::table('products')
        ->where('subcategory_id', $id)
        ->join('product_subspecification', 'product_subspecification.product_id', '=', 'products.id')
        ->join('subspecifications', function ($keys) {
            $keys->on('subspecifications.id', '=', 'product_subspecification.subspecification_id')
            ->Status('Active');
        })
        ->groupBy('subspecifications.id')
        ->get();

    return response()->json($specifications);

}

如果我删除->Status('Active')这是我的范围,

scope

public function scopeStatus($query, $status)
{
      $statuses = Status::where('title', $status)->pluck('id'); // Or relevant column name
      return $query->whereIn('status_id', $statuses);
}
  

如何在此查询中实现我的范围?

1 个答案:

答案 0 :(得分:0)

Scopes应始终与查询构建器实例链接。您提供的代码$keyJoinClauseAPI),不应与范围链接。

$specifications = DB::table('products')
    ->where('subcategory_id', $id)
    ->join('product_subspecification', 'product_subspecification.product_id', '=', 'products.id')
    ->join('subspecifications', function ($keys) {
        $keys->on('subspecifications.id', '=', 'product_subspecification.subspecification_id');
    })
    ->status('Active') // chain with query builder
    ->groupBy('subspecifications.id')
    ->get();