category demo
This commit is contained in:
parent
56c5aea7e6
commit
67e1098402
|
|
@ -0,0 +1,30 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Requests;
|
||||||
|
|
||||||
|
use Illuminate\Foundation\Http\FormRequest;
|
||||||
|
|
||||||
|
class ContactRequest extends FormRequest
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Determine if the user is authorized to make this request.
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function authorize()
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the validation rules that apply to the request.
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function rules()
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'name'=>'required|string|max:100|min:10'
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -4,10 +4,18 @@ namespace Beike\Shop\Http\Controllers;
|
||||||
|
|
||||||
use Beike\Models\Category;
|
use Beike\Models\Category;
|
||||||
use Beike\Models\Product;
|
use Beike\Models\Product;
|
||||||
|
use Beike\Shop\Http\Resources\CategoryItem;
|
||||||
|
use Beike\Shop\Repositories\CategoryRepo;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
|
|
||||||
class CategoryController extends Controller
|
class CategoryController extends Controller
|
||||||
{
|
{
|
||||||
|
public function index(Request $request)
|
||||||
|
{
|
||||||
|
$items = CategoryRepo::list();
|
||||||
|
return CategoryItem::collection($items);
|
||||||
|
}
|
||||||
|
|
||||||
public function show(Request $request, Category $category)
|
public function show(Request $request, Category $category)
|
||||||
{
|
{
|
||||||
$products = Product::query()
|
$products = Product::query()
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,22 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Beike\Shop\Http\Resources;
|
||||||
|
|
||||||
|
use Illuminate\Http\Resources\Json\JsonResource;
|
||||||
|
|
||||||
|
class CategoryItem extends JsonResource
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Transform the resource into an array.
|
||||||
|
*
|
||||||
|
* @param \Illuminate\Http\Request $request
|
||||||
|
* @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
|
||||||
|
*/
|
||||||
|
public function toArray($request)
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'id' => $this->id,
|
||||||
|
'name' => $this->description->name ?? ''
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,63 @@
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* CategoryRepo.php
|
||||||
|
*
|
||||||
|
* @copyright 2022 opencart.cn - All Rights Reserved
|
||||||
|
* @link http://www.guangdawangluo.com
|
||||||
|
* @author Edward Yang <yangjin@opencart.cn>
|
||||||
|
* @created 2022-06-16 17:45:41
|
||||||
|
* @modified 2022-06-16 17:45:41
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Beike\Shop\Repositories;
|
||||||
|
|
||||||
|
use Beike\Models\Category;
|
||||||
|
use Illuminate\Database\Eloquent\Builder;
|
||||||
|
use Illuminate\Database\Eloquent\Collection;
|
||||||
|
|
||||||
|
class CategoryRepo
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* 获取产品分类列表
|
||||||
|
*
|
||||||
|
* @param array $filter , keyword, a_name, b_name, category_page, per_page
|
||||||
|
* @return Builder[]|Collection
|
||||||
|
*/
|
||||||
|
public static function list(array $filter = [])
|
||||||
|
{
|
||||||
|
$keyword = $filter['keyword']??'';
|
||||||
|
$builder = Category::query()->with(['description']);
|
||||||
|
if ($keyword) {
|
||||||
|
// $builder->whereExists('name')
|
||||||
|
}
|
||||||
|
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 multipleUpdate()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
@ -10,6 +10,7 @@ Route::prefix('/')
|
||||||
|
|
||||||
Route::get('carts', [Beike\Shop\Http\Controllers\CartController::class, 'store'])->name('carts.store');
|
Route::get('carts', [Beike\Shop\Http\Controllers\CartController::class, 'store'])->name('carts.store');
|
||||||
|
|
||||||
|
Route::get('categories', [Beike\Shop\Http\Controllers\CategoryController::class, 'index'])->name('categories.index');
|
||||||
Route::get('categories/{category}', [Beike\Shop\Http\Controllers\CategoryController::class, 'show'])->name('categories.show');
|
Route::get('categories/{category}', [Beike\Shop\Http\Controllers\CategoryController::class, 'show'])->name('categories.show');
|
||||||
|
|
||||||
Route::get('products/{product}', [Beike\Shop\Http\Controllers\ProductController::class, 'show'])->name('products.show');
|
Route::get('products/{product}', [Beike\Shop\Http\Controllers\ProductController::class, 'show'])->name('products.show');
|
||||||
|
|
|
||||||
|
|
@ -175,8 +175,8 @@ return [
|
||||||
App\Providers\EventServiceProvider::class,
|
App\Providers\EventServiceProvider::class,
|
||||||
App\Providers\RouteServiceProvider::class,
|
App\Providers\RouteServiceProvider::class,
|
||||||
|
|
||||||
\Beike\Admin\Providers\AdminServiceProvider::class,
|
Beike\Admin\Providers\AdminServiceProvider::class,
|
||||||
\Beike\Shop\Providers\ShopServiceProvider::class,
|
Beike\Shop\Providers\ShopServiceProvider::class,
|
||||||
|
|
||||||
],
|
],
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue