admin products
This commit is contained in:
parent
c688f18ac8
commit
42830e4938
|
|
@ -3,81 +3,56 @@
|
|||
namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Product;
|
||||
use App\Services\ProductService;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Response;
|
||||
|
||||
class ProductsController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
return view('admin.pages.products.index');
|
||||
$products = Product::query()
|
||||
->withCount('skus')
|
||||
->paginate();
|
||||
|
||||
$data = [
|
||||
'products' => $products,
|
||||
];
|
||||
|
||||
return view('admin.pages.products.index', $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for creating a new resource.
|
||||
*
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
//
|
||||
return view('admin.pages.products.form.form');
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
//
|
||||
$product = (new ProductService)->create($request->all());
|
||||
|
||||
return redirect()->route('admin.products.index')->with('success', 'product created');
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the specified resource.
|
||||
*
|
||||
* @param int $id
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function show($id)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for editing the specified resource.
|
||||
*
|
||||
* @param int $id
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function edit($id)
|
||||
public function edit(Product $product)
|
||||
{
|
||||
//
|
||||
$data = [
|
||||
'product' => $product,
|
||||
];
|
||||
return view('admin.pages.products.form.form', $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param int $id
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function update(Request $request, $id)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*
|
||||
* @param int $id
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function destroy($id)
|
||||
{
|
||||
//
|
||||
|
|
|
|||
|
|
@ -20,7 +20,7 @@ class Kernel extends HttpKernel
|
|||
\App\Http\Middleware\PreventRequestsDuringMaintenance::class,
|
||||
\Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
|
||||
\App\Http\Middleware\TrimStrings::class,
|
||||
\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
|
||||
// \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
|
||||
];
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -15,4 +15,9 @@ class Product extends Model
|
|||
{
|
||||
return $this->hasMany(ProductSku::class);
|
||||
}
|
||||
|
||||
public function getVariableDecodedAttribute()
|
||||
{
|
||||
return json_decode($this->variable, true);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,38 @@
|
|||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Models\Product;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class ProductService
|
||||
{
|
||||
public function create($data)
|
||||
{
|
||||
try {
|
||||
DB::beginTransaction();
|
||||
|
||||
$product = new Product($data);
|
||||
if (isset($data['variant'])) {
|
||||
$product->variable = json_encode($data['variant']);
|
||||
}
|
||||
$product->saveOrFail();
|
||||
|
||||
$skus = [];
|
||||
foreach ($data['skus'] as $index => $rawSku) {
|
||||
$sku = $rawSku;
|
||||
$sku['is_default'] = $index == 0;
|
||||
$skus[] = $sku;
|
||||
}
|
||||
|
||||
$product->skus()->createMany($skus);
|
||||
|
||||
DB::commit();
|
||||
|
||||
return $product;
|
||||
} catch (\Exception $e) {
|
||||
DB::rollBack();
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -6,8 +6,11 @@
|
|||
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
|
||||
<meta http-equiv="X-UA-Compatible" content="ie=edge">
|
||||
<title>Document</title>
|
||||
@stack('header')
|
||||
</head>
|
||||
<body>
|
||||
|
||||
@yield('content')
|
||||
</body>
|
||||
|
||||
@stack('footer')
|
||||
</html>
|
||||
|
|
|
|||
|
|
@ -1 +1,5 @@
|
|||
@extends('admin.layouts.master')
|
||||
|
||||
@section('content')
|
||||
<a href="{{ route('admin.products.index') }}">Products</a>
|
||||
@endsection
|
||||
|
|
@ -0,0 +1,76 @@
|
|||
@extends('admin.layouts.master')
|
||||
|
||||
@push('header')
|
||||
<script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.14/vue.js"></script>
|
||||
@endpush
|
||||
|
||||
@section('content')
|
||||
<h2>product</h2>
|
||||
<form action="{{ route('admin.products.store') }}" method="POST" id="app">
|
||||
@csrf
|
||||
|
||||
<input type="text" name="name" placeholder="Name" value="{{ old('name', $product->name ?? '') }}">
|
||||
<input type="text" name="image" placeholder="image" value="{{ old('image', $product->image ?? '') }}">
|
||||
<input type="text" name="video" placeholder="video" value="{{ old('video', $product->video ?? '') }}">
|
||||
<input type="text" name="sort_order" placeholder="sort_order" value="{{ old('sort_order', $product->sort_order ?? 0) }}">
|
||||
<input type="text" name="status" placeholder="status" value="{{ old('status', $product->status ?? 1) }}">
|
||||
|
||||
<div>
|
||||
<h2>skus</h2>
|
||||
<input type="radio" v-model="editing.isVariable" :value="false"> 单规格
|
||||
<input type="radio" v-model="editing.isVariable" :value="true"> 多规格
|
||||
<div v-if="editing.isVariable">
|
||||
<div>
|
||||
<div v-for="variant in form.variable">
|
||||
<input type="text" v-model="variant.name" name="variant[0][name]" placeholder="variant name">
|
||||
|
||||
<input v-for="(value, valueIndex) in variant.values" v-model="variant.values[valueIndex]" type="text" name="variant[0][values][0]" placeholder="variant value name">
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<table>
|
||||
<tr v-for="(sku, skuIndex) in form.skus">
|
||||
<td><input type="text" v-model="sku.image" name="skus[0][image]" placeholder="image"></td>
|
||||
<td><input type="text" v-model="sku.model" name="skus[0][model]" placeholder="model"></td>
|
||||
<td><input type="text" v-model="sku.sku" name="skus[0][sku]" placeholder="sku"></td>
|
||||
<td><input type="text" v-model="sku.price" name="skus[0][price]" placeholder="price" value="10"></td>
|
||||
<td><input type="text" v-model="sku.quantity" name="skus[0][quantity]" placeholder="quantity" value="10"></td>
|
||||
<td><input type="text" v-model="sku.is_default" name="skus[0][is_default]" placeholder="is_default" value="1"></td>
|
||||
</tr>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div v-if="!editing.isVariable">
|
||||
<div>
|
||||
<input type="text" name="skus[0][image]" placeholder="image">
|
||||
<input type="text" name="skus[0][model]" placeholder="model">
|
||||
<input type="text" name="skus[0][sku]" placeholder="sku">
|
||||
<input type="text" name="skus[0][price]" placeholder="price" value="10">
|
||||
<input type="text" name="skus[0][quantity]" placeholder="quantity" value="10">
|
||||
<input type="text" name="skus[0][is_default]" placeholder="is_default" value="1">
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<button type="submit">Save</button>
|
||||
</form>
|
||||
@endsection
|
||||
|
||||
@push('footer')
|
||||
<script>
|
||||
new Vue({
|
||||
el: '#app',
|
||||
data: {
|
||||
form: {
|
||||
variable: @json($product->variable_decoded ?? []),
|
||||
skus: @json($product->skus ?? []),
|
||||
},
|
||||
editing: {
|
||||
isVariable: @json(($product->variable ?? null) != null),
|
||||
}
|
||||
}
|
||||
});
|
||||
</script>
|
||||
@endpush
|
||||
|
|
@ -1 +1,16 @@
|
|||
@extends('admin.pages.products.index')
|
||||
@extends('admin.layouts.master')
|
||||
|
||||
@section('content')
|
||||
<a href="{{ route('admin.products.create') }}">Create</a>
|
||||
<table>
|
||||
@foreach ($products as $product)
|
||||
<tr>
|
||||
<td>{{ $product->id }}</td>
|
||||
<td>{{ $product->variable ? '多规格' : '单规格' }}</td>
|
||||
<td>
|
||||
<a href="{{ route('admin.products.edit', $product) }}">编辑</a>
|
||||
</td>
|
||||
</tr>
|
||||
@endforeach
|
||||
</table>
|
||||
@endsection
|
||||
|
|
|
|||
Loading…
Reference in New Issue