fixed product list
This commit is contained in:
parent
ca1ab01624
commit
4d408c2d50
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
namespace Beike\Admin\Repositories;
|
namespace Beike\Admin\Repositories;
|
||||||
|
|
||||||
|
use Beike\Models\Category;
|
||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
|
|
||||||
class CategoryRepo
|
class CategoryRepo
|
||||||
|
|
@ -18,4 +19,21 @@ class CategoryRepo
|
||||||
|
|
||||||
return $results;
|
return $results;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $categoryData
|
||||||
|
*/
|
||||||
|
public static function create($categoryData)
|
||||||
|
{
|
||||||
|
// Category::query()->create($categoryData);
|
||||||
|
$category = new Category();
|
||||||
|
$category->parent_id = 12;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function update(Category $category)
|
||||||
|
{
|
||||||
|
$category->update([
|
||||||
|
|
||||||
|
]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ namespace Beike\Shop\Http\Controllers;
|
||||||
|
|
||||||
use Beike\Models\Category;
|
use Beike\Models\Category;
|
||||||
use Beike\Shop\Repositories\CategoryRepo;
|
use Beike\Shop\Repositories\CategoryRepo;
|
||||||
|
use Beike\Shop\Repositories\ProductRepo;
|
||||||
use Plugin\Guangda\Seller\Models\Product;
|
use Plugin\Guangda\Seller\Models\Product;
|
||||||
|
|
||||||
class HomeController extends Controller
|
class HomeController extends Controller
|
||||||
|
|
@ -12,6 +13,7 @@ class HomeController extends Controller
|
||||||
{
|
{
|
||||||
$data = [
|
$data = [
|
||||||
'categories' => CategoryRepo::getTwoLevelCategories(),
|
'categories' => CategoryRepo::getTwoLevelCategories(),
|
||||||
|
'category_products' => ProductRepo::getProductsByCategories([100002,100003]),
|
||||||
];
|
];
|
||||||
|
|
||||||
return view('home', $data);
|
return view('home', $data);
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@ namespace Beike\Shop\Http\Resources;
|
||||||
|
|
||||||
use Illuminate\Http\Resources\Json\JsonResource;
|
use Illuminate\Http\Resources\Json\JsonResource;
|
||||||
|
|
||||||
class CategoryItem extends JsonResource
|
class CategoryList extends JsonResource
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Transform the resource into an array.
|
* Transform the resource into an array.
|
||||||
|
|
@ -19,7 +19,7 @@ class CategoryItem extends JsonResource
|
||||||
'name' => $this->description->name ?? '',
|
'name' => $this->description->name ?? '',
|
||||||
|
|
||||||
];
|
];
|
||||||
if ($this->children) {
|
if ($this->children->count() > 0) {
|
||||||
$item['children'] = self::collection($this->children);
|
$item['children'] = self::collection($this->children);
|
||||||
}
|
}
|
||||||
return $item;
|
return $item;
|
||||||
|
|
@ -0,0 +1,26 @@
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* ProductList.php
|
||||||
|
*
|
||||||
|
* @copyright 2022 opencart.cn - All Rights Reserved
|
||||||
|
* @link http://www.guangdawangluo.com
|
||||||
|
* @author Edward Yang <yangjin@opencart.cn>
|
||||||
|
* @created 2022-06-23 11:33:06
|
||||||
|
* @modified 2022-06-23 11:33:06
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Beike\Shop\Http\Resources;
|
||||||
|
|
||||||
|
use Illuminate\Http\Resources\Json\JsonResource;
|
||||||
|
|
||||||
|
class ProductList extends JsonResource
|
||||||
|
{
|
||||||
|
public function toArray($request): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'id' => $this->id,
|
||||||
|
'name' => $this->description->name ?? '',
|
||||||
|
'category_id' => $this->category_id ?? null,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -12,6 +12,7 @@
|
||||||
namespace Beike\Shop\Repositories;
|
namespace Beike\Shop\Repositories;
|
||||||
|
|
||||||
use Beike\Models\Category;
|
use Beike\Models\Category;
|
||||||
|
use Beike\Shop\Http\Resources\CategoryList;
|
||||||
use Illuminate\Database\Eloquent\Builder;
|
use Illuminate\Database\Eloquent\Builder;
|
||||||
use Illuminate\Database\Eloquent\Collection;
|
use Illuminate\Database\Eloquent\Collection;
|
||||||
|
|
||||||
|
|
@ -22,30 +23,14 @@ class CategoryRepo
|
||||||
*/
|
*/
|
||||||
public static function getTwoLevelCategories()
|
public static function getTwoLevelCategories()
|
||||||
{
|
{
|
||||||
$items = [];
|
|
||||||
$topCategories = Category::query()
|
$topCategories = Category::query()
|
||||||
->from('categories as c')
|
->from('categories as c')
|
||||||
->with(['description', 'children.description'])
|
->with(['description', 'children.description'])
|
||||||
->where('parent_id', 0)
|
->where('parent_id', 0)
|
||||||
->get();
|
->get();
|
||||||
|
|
||||||
foreach ($topCategories as $index => $topCategory) {
|
$categoryList = CategoryList::collection($topCategories);
|
||||||
$items[$index] = [
|
return json_decode($categoryList->toJson(), true);
|
||||||
'id' => $topCategory->id,
|
|
||||||
'name' => $topCategory->description->name
|
|
||||||
];
|
|
||||||
$children = $topCategory->children;
|
|
||||||
if ($children->count() > 0) {
|
|
||||||
foreach ($children as $itemIndex => $item) {
|
|
||||||
$items[$index]['children'][$itemIndex] = [
|
|
||||||
'id' => $item->id,
|
|
||||||
'name' => $item->description->name
|
|
||||||
];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return $items;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -65,23 +50,6 @@ class CategoryRepo
|
||||||
return $builder->get();
|
return $builder->get();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $categoryData
|
|
||||||
*/
|
|
||||||
public static function create($categoryData)
|
|
||||||
{
|
|
||||||
// Category::query()->create($categoryData);
|
|
||||||
$category = new Category();
|
|
||||||
$category->parent_id = 12;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static function update(Category $category)
|
|
||||||
{
|
|
||||||
$category->update([
|
|
||||||
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static function updateViewNumber()
|
public static function updateViewNumber()
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,54 @@
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* ProductRepo.php
|
||||||
|
*
|
||||||
|
* @copyright 2022 opencart.cn - All Rights Reserved
|
||||||
|
* @link http://www.guangdawangluo.com
|
||||||
|
* @author Edward Yang <yangjin@opencart.cn>
|
||||||
|
* @created 2022-06-23 11:19:23
|
||||||
|
* @modified 2022-06-23 11:19:23
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Beike\Shop\Repositories;
|
||||||
|
|
||||||
|
use Beike\Models\Product;
|
||||||
|
use Beike\Shop\Http\Resources\ProductList;
|
||||||
|
use Illuminate\Support\Collection;
|
||||||
|
|
||||||
|
class ProductRepo
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* 通过多个产品分类获取产品列表
|
||||||
|
*
|
||||||
|
* @param $categoryIds
|
||||||
|
* @return Collection
|
||||||
|
*/
|
||||||
|
public static function getProductsByCategories($categoryIds): Collection
|
||||||
|
{
|
||||||
|
$products = self::getProductsByCategory($categoryIds);
|
||||||
|
$items = collect($products)->groupBy('category_id');
|
||||||
|
return $items;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通过单个产品分类获取产品列表
|
||||||
|
*
|
||||||
|
* @param $categoryId
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public static function getProductsByCategory($categoryId): array
|
||||||
|
{
|
||||||
|
if (is_int($categoryId)) {
|
||||||
|
$categoryId[] = $categoryId;
|
||||||
|
}
|
||||||
|
$products = Product::query()
|
||||||
|
->select(['products.*', 'pc.category_id'])
|
||||||
|
->join('product_categories as pc', 'products.id', '=', 'pc.product_id')
|
||||||
|
->join('categories as c', 'pc.category_id', '=', 'c.id')
|
||||||
|
->whereIn('c.id', $categoryId)
|
||||||
|
->get();
|
||||||
|
|
||||||
|
return ProductList::collection($products)->jsonSerialize();
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue