From 63efb506f7349f3c20482af9346757e9bbe7f89c Mon Sep 17 00:00:00 2001 From: TL Date: Tue, 2 Aug 2022 18:44:40 +0800 Subject: [PATCH] =?UTF-8?q?=E5=93=81=E7=89=8C=E6=A0=B9=E6=8D=AEid=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E5=90=8D=E5=AD=97=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Http/Controllers/BrandController.php | 35 ++++++++- beike/Admin/Routes/admin.php | 5 +- beike/Models/Customer.php | 5 ++ beike/Repositories/BrandRepo.php | 73 ++++++++++++++++--- 4 files changed, 102 insertions(+), 16 deletions(-) diff --git a/beike/Admin/Http/Controllers/BrandController.php b/beike/Admin/Http/Controllers/BrandController.php index ab464947..3482fe84 100644 --- a/beike/Admin/Http/Controllers/BrandController.php +++ b/beike/Admin/Http/Controllers/BrandController.php @@ -12,6 +12,7 @@ namespace Beike\Admin\Http\Controllers; use Beike\Repositories\BrandRepo; +use Exception; use Illuminate\Http\Request; class BrandController extends Controller @@ -30,30 +31,56 @@ class BrandController extends Controller return view('admin::pages.brands.index', $data); } - public function store(Request $request) + public function store(Request $request): array { $brand = BrandRepo::create($request->all()); return json_success("创建成功", $brand); } - public function update(Request $request, int $id) + /** + * @throws Exception + */ + public function update(Request $request, int $id): array { $brand = BrandRepo::update($id, $request->all()); return json_success("成功修改", $brand); } - public function destroy(int $addressId) + public function destroy(int $addressId): array { BrandRepo::delete($addressId); return json_success("已成功删除"); } - public function autocomplete(Request $request) + public function autocomplete(Request $request): array { $brands = BrandRepo::autocomplete($request->get('name') ?? ''); return json_success('获取成功!', $brands); } + + + public function name(int $id): array + { + $name = BrandRepo::getName($id); + + return json_success('获取成功', $name); + } + + + /** + * 根据产品ID批量获取产品名称 + * + * @param Request $request + * @return array + */ + public function getNames(Request $request): array + { + $ids = explode(',',$request->get('ids')); + $name = BrandRepo::getNames($ids); + + return json_success('获取成功', $name); + } } diff --git a/beike/Admin/Routes/admin.php b/beike/Admin/Routes/admin.php index 0da90d0e..1ffdf50e 100644 --- a/beike/Admin/Routes/admin.php +++ b/beike/Admin/Routes/admin.php @@ -24,6 +24,10 @@ Route::prefix($adminName) ->group(function () { Route::get('/', [Controllers\HomeController::class, 'index'])->name('home.index'); + Route::resource('brands', Controllers\BrandController::class); + Route::get('brands/{id}/name', [Controllers\BrandController::class, 'name'])->name('brands.name'); + Route::get('brands/names', [Controllers\BrandController::class, 'getNames'])->name('brands.names'); + Route::resource('categories', Controllers\CategoryController::class); Route::get('categories/{id}/name', [Controllers\CategoryController::class, 'name'])->name('categories.name'); @@ -39,7 +43,6 @@ Route::prefix($adminName) Route::post('design/builder/preview', [Controllers\DesignController::class, 'preview'])->name('design.module.preview'); Route::resource('files', Controllers\FileController::class); - Route::resource('brands', Controllers\BrandController::class); Route::get('file_manager', [Controllers\FileManagerController::class, 'index'])->name('file_manager.index'); Route::get('file_manager/files', [Controllers\FileManagerController::class, 'getFiles'])->name('file_manager.get_files'); diff --git a/beike/Models/Customer.php b/beike/Models/Customer.php index b0a9f67a..7c0d0a6c 100644 --- a/beike/Models/Customer.php +++ b/beike/Models/Customer.php @@ -32,4 +32,9 @@ class Customer extends Authenticatable { return $this->hasMany(CustomerWishlist::class); } + + public function rmas() : HasMany + { + return $this->hasMany(Rma::class); + } } diff --git a/beike/Repositories/BrandRepo.php b/beike/Repositories/BrandRepo.php index 1fdd2f42..176d7b2c 100644 --- a/beike/Repositories/BrandRepo.php +++ b/beike/Repositories/BrandRepo.php @@ -13,25 +13,30 @@ namespace Beike\Repositories; use Beike\Models\Brand; use Beike\Shop\Http\Resources\BrandDetail; +use Exception; +use Illuminate\Contracts\Pagination\LengthAwarePaginator; +use Illuminate\Database\Eloquent\Builder; +use Illuminate\Database\Eloquent\Collection; +use Illuminate\Database\Eloquent\HigherOrderBuilderProxy; +use Illuminate\Database\Eloquent\Model; class BrandRepo { /** * 创建一个记录 * @param $data - * @return int + * @return Builder|Model */ public static function create($data) { - $brand = Brand::query()->create($data); - return $brand; + return Brand::query()->create($data); } /** * @param $brand * @param $data - * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Model|mixed - * @throws \Exception + * @return Builder|Builder[]|Collection|Model|mixed + * @throws Exception */ public static function update($brand, $data) { @@ -39,7 +44,7 @@ class BrandRepo $brand = Brand::query()->find($brand); } if (!$brand) { - throw new \Exception("品牌id {$brand} 不存在"); + throw new Exception("品牌id $brand 不存在"); } $brand->update($data); return $brand; @@ -47,7 +52,7 @@ class BrandRepo /** * @param $id - * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Model|null + * @return Builder|Builder[]|Collection|Model|null */ public static function find($id) { @@ -68,9 +73,9 @@ class BrandRepo /** * @param $data - * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator + * @return LengthAwarePaginator */ - public static function list($data) + public static function list($data): LengthAwarePaginator { $builder = Brand::query(); @@ -87,7 +92,7 @@ class BrandRepo return $builder->paginate(10)->withQueryString(); } - public static function listGroupByFirst() + public static function listGroupByFirst(): array { $brands = Brand::query()->where('status', true)->get(); @@ -102,10 +107,56 @@ class BrandRepo public static function autocomplete($name) { $brands = Brand::query() - ->where('name', 'like', "{$name}%") + ->where('name', 'like', "$name%") ->where('status', 1) ->select('id', 'name') ->limit(10)->get(); return $brands; } + + /** + * 获取商品名称 + * @param $id + * @return HigherOrderBuilderProxy|mixed|string + */ + public static function getName($id) + { + $brand = Brand::query()->find($id); + + if ($brand) { + return $brand->name; + } + return ''; + } + + + /** + * @param $ids + * @return array + */ + public static function getNames($ids): array + { + $brands = self::getListByIds($ids); + return $brands->map(function ($brand) { + return [ + 'id' => $brand->id, + 'name' => $brand->name ?? '' + ]; + })->toArray(); + } + + /** + * 通过产品ID获取产品列表 + * @return array|Builder[]|Collection + */ + public static function getListByIds($ids) + { + if (empty($ids)) { + return []; + } + $brands = Brand::query() + ->whereIn('id', $ids) + ->get(); + return $brands; + } }