Optimize brand.
This commit is contained in:
parent
024a2190f2
commit
7e934151f0
|
|
@ -11,6 +11,7 @@
|
|||
|
||||
namespace Beike\Admin\Http\Controllers;
|
||||
|
||||
use Beike\Models\Brand;
|
||||
use Beike\Repositories\BrandRepo;
|
||||
use Exception;
|
||||
use Illuminate\Http\Request;
|
||||
|
|
@ -57,30 +58,43 @@ class BrandController extends Controller
|
|||
}
|
||||
|
||||
/**
|
||||
* @param Request $request
|
||||
* @param Brand $brand
|
||||
* @return array
|
||||
* @throws Exception
|
||||
*/
|
||||
public function update(Request $request, int $id): array
|
||||
public function update(Request $request, Brand $brand): array
|
||||
{
|
||||
$requestData = $request->all();
|
||||
$data = [
|
||||
'brand_id' => $id,
|
||||
'brand_id' => $brand,
|
||||
'request_data' => $requestData,
|
||||
];
|
||||
hook_action('admin.brand.update.before', $data);
|
||||
$brand = BrandRepo::update($id, $requestData);
|
||||
$brand = BrandRepo::update($brand, $requestData);
|
||||
hook_action('admin.brand.update.after', $data);
|
||||
|
||||
return json_success(trans('common.updated_success'), $brand);
|
||||
}
|
||||
|
||||
public function destroy(int $brandId): array
|
||||
/**
|
||||
* @param Request $request
|
||||
* @param Brand $brand
|
||||
* @return array
|
||||
*/
|
||||
public function destroy(Request $request, Brand $brand): array
|
||||
{
|
||||
BrandRepo::delete($brandId);
|
||||
hook_action('admin.brand.destroy.after', $brandId);
|
||||
hook_action('admin.brand.destroy.before', $brand);
|
||||
BrandRepo::delete($brand);
|
||||
hook_action('admin.brand.destroy.after', $brand);
|
||||
|
||||
return json_success(trans('common.deleted_success'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Request $request
|
||||
* @return array
|
||||
*/
|
||||
public function autocomplete(Request $request): array
|
||||
{
|
||||
$brands = BrandRepo::autocomplete($request->get('name') ?? '', 0);
|
||||
|
|
@ -88,6 +102,10 @@ class BrandController extends Controller
|
|||
return json_success(trans('common.get_success'), $brands);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $id
|
||||
* @return array
|
||||
*/
|
||||
public function name(int $id): array
|
||||
{
|
||||
$name = BrandRepo::getName($id);
|
||||
|
|
|
|||
|
|
@ -44,8 +44,8 @@ Route::prefix($adminName)
|
|||
Route::middleware('can:brands_show')->get('brands/{id}/name', [Controllers\BrandController::class, 'name'])->name('brands.name');
|
||||
Route::middleware('can:brands_index')->get('brands', [Controllers\BrandController::class, 'index'])->name('brands.index');
|
||||
Route::middleware('can:brands_create')->post('brands', [Controllers\BrandController::class, 'store'])->name('brands.store');
|
||||
Route::middleware('can:brands_update')->put('brands/{id}', [Controllers\BrandController::class, 'update'])->name('brands.update');
|
||||
Route::middleware('can:brands_delete')->delete('brands/{id}', [Controllers\BrandController::class, 'destroy'])->name('brands.destroy');
|
||||
Route::middleware('can:brands_update')->put('brands/{brand}', [Controllers\BrandController::class, 'update'])->name('brands.update');
|
||||
Route::middleware('can:brands_delete')->delete('brands/{brand}', [Controllers\BrandController::class, 'destroy'])->name('brands.destroy');
|
||||
|
||||
// 商品分类
|
||||
Route::middleware('can:categories_index')->get('categories/autocomplete', [Controllers\CategoryController::class, 'autocomplete'])->name('categories.autocomplete');
|
||||
|
|
|
|||
|
|
@ -78,12 +78,15 @@ class BrandRepo
|
|||
}
|
||||
|
||||
/**
|
||||
* @param $id
|
||||
* @param $brand
|
||||
* @return void
|
||||
*/
|
||||
public static function delete($id)
|
||||
public static function delete($brand)
|
||||
{
|
||||
$brand = Brand::query()->find($id);
|
||||
if (! $brand instanceof Brand) {
|
||||
$brand = Brand::query()->find((int) $brand);
|
||||
}
|
||||
|
||||
if ($brand) {
|
||||
$brand->delete();
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue