4
0
Fork 0

goods filter

master
panliang 2022-09-13 14:11:03 +08:00
parent 55cc3ace9a
commit 2d40e77720
6 changed files with 8 additions and 38 deletions

View File

@ -40,6 +40,8 @@ $menus = [
];
```
## API接口
## 数据表
### 商品分类: goods_category

View File

@ -3,7 +3,6 @@
namespace Peidikeji\Goods\Filters;
use EloquentFilter\ModelFilter;
use Peidikeji\Merchant\Enums\CheckStatus;
class GoodsFilter extends ModelFilter
{
@ -19,15 +18,6 @@ class GoodsFilter extends ModelFilter
});
}
public function merchant($v)
{
if (! $v) {
$this->whereNull('merchant_id');
} else {
$this->where('merchant_id', $v);
}
}
public function recommend($v)
{
$this->where('is_recommend', $v);
@ -35,20 +25,12 @@ class GoodsFilter extends ModelFilter
public function category($v)
{
$this->where('category_id', $v);
// $this->where('category_id', $v);
$this->whereHas('category', fn($q) => $q->where('id', $v)->orWhere('path', 'like', '%-'.$v.'-%'));
}
public function sort($v)
{
$this->orderBy($v, request('sort_by', 'asc'));
}
public function state($state): GoodsFilter
{
return match ((int) $state) {
default => $this->where('on_sale', true),
2 => $this->where('check_status', CheckStatus::Processing),
3 => $this->where('on_sale', false)
};
}
}

View File

@ -3,21 +3,13 @@
namespace Peidikeji\Goods\Http\Api;
use App\Http\Controllers\Controller;
use Peidikeji\Goods\Http\Resources\GoodsCategoryResource;
use Peidikeji\Goods\Models\GoodsCategory;
class GoodsCategoryController extends Controller
{
public function index()
{
$query = GoodsCategory::with([
'children' => function ($q) {
$q->show()->sort();
},
])->filter(['level' => 2])->show()->sort();
$list = $query->get();
return $this->json(GoodsCategoryResource::collection($list));
$data = (new GoodsCategory())->toTree(GoodsCategory::show()->get()->all());
return $this->json($data);
}
}

View File

@ -13,7 +13,7 @@ class GoodsController extends Controller
{
public function index(Request $request)
{
$query = Goods::with(['merchant'])->filter($request->all());
$query = Goods::filter($request->all());
$list = $query->show()->sort()->paginate($request->input('per_page'));

View File

@ -3,7 +3,6 @@
namespace Peidikeji\Goods\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Str;
class GoodsCategoryResource extends JsonResource
{
@ -19,8 +18,6 @@ class GoodsCategoryResource extends JsonResource
'path' => $this->path,
'children' => static::collection($this->whenLoaded('children')),
'type' => Str::startsWith($this->path, '-2-') ? 'shop' : '',
];
}
}

View File

@ -11,10 +11,8 @@ class GoodsTinyResource extends JsonResource
{
return [
'id' => $this->id,
'merchant_id' => $this->merchant_id,
'merchant' => MerchantTinyResource::make($this->whenLoaded('merchant')),
'category_id' => $this->category_id,
'type_id' => $this->category_id,
'type_id' => $this->type_id,
'brand_id' => $this->brand_id,
'goods_sn' => $this->goods_sn,
'name' => $this->name,
@ -22,7 +20,6 @@ class GoodsTinyResource extends JsonResource
'images' => $this->images,
'description' => $this->description,
'price' => floatval($this->price),
'vip_price' => floatval($this->vip_price),
'on_sale' => $this->on_sale,
'sold_count' => $this->sold_count,
'stock' => $this->stock,