品牌根据id获取名字接口
This commit is contained in:
parent
25ec233e16
commit
63efb506f7
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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');
|
||||
|
|
|
|||
|
|
@ -32,4 +32,9 @@ class Customer extends Authenticatable
|
|||
{
|
||||
return $this->hasMany(CustomerWishlist::class);
|
||||
}
|
||||
|
||||
public function rmas() : HasMany
|
||||
{
|
||||
return $this->hasMany(Rma::class);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue