这是我的功能:
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);
}
如何在此查询中实现我的范围?
答案 0 :(得分:0)
Scopes
应始终与查询构建器实例链接。您提供的代码$key
为JoinClause
(API),不应与范围链接。
$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();