From 1bd0c6ebe84821ae10f026e65b05db4495cdad3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BD=D0=B4=D1=80=D0=B5=D0=B9=20=20=D0=9B=D0=B0=D1=80?= =?UTF-8?q?=D0=B8=D0=BE=D0=BD=D0=BE=D0=B2?= Date: Wed, 5 Apr 2023 20:46:17 +0700 Subject: [PATCH] =?UTF-8?q?=D0=90=D0=B4=D0=BC=D0=B8=D0=BD=D0=BA=D0=B0,=20=D1?= =?UTF-8?q?=82=D0=BE=D0=B2=D0=B0=D1=80=D1=8B.=20=D0=A4=D1=80=D0=BE=D0=BD=D0=B4?= =?UTF-8?q?=D1=8D=D0=BD=D0=B4=20=D0=B3=D0=BB=D0=B0=D0=B2=D0=BD=D0=B0=D1=8F=20?= =?UTF-8?q?=D1=81=D1=82=D1=80=D0=B0=D0=BD=D0=B8=D1=86=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Http/Controllers/Admin/GoodController.php | 39 +++- app/Http/Controllers/MainController.php | 9 +- app/Http/Requests/GoodsRequest.php | 39 ++++ app/Models/Category.php | 4 + app/Models/Good.php | 11 + app/Models/News.php | 2 + app/Models/Project.php | 2 + app/Providers/ComposerServiceProvider.php | 16 ++ .../2023_03_31_121853_create_goods_table.php | 1 + .../2023_03_31_122437_create_news_table.php | 3 + resources/views/admin/goods/create.blade.php | 15 ++ resources/views/admin/goods/edit.blade.php | 14 ++ resources/views/admin/goods/form.blade.php | 237 ++++++++++++++++++++ resources/views/admin/goods/index.blade.php | 66 ++++++ resources/views/admin/news/edit.blade.php | 2 +- resources/views/admin/part/category_id.blade.php | 10 + resources/views/admin/part/goods.blade.php | 7 + resources/views/banners/index.blade.php | 21 +- resources/views/index.blade.php | 12 +- resources/views/layout/admin.blade.php | 2 +- routes/web.php | 3 +- 21 files changed, 491 insertions(+), 24 deletions(-) create mode 100644 app/Http/Requests/GoodsRequest.php create mode 100644 resources/views/admin/goods/create.blade.php create mode 100644 resources/views/admin/goods/edit.blade.php create mode 100644 resources/views/admin/goods/form.blade.php create mode 100644 resources/views/admin/goods/index.blade.php create mode 100644 resources/views/admin/part/category_id.blade.php create mode 100644 resources/views/admin/part/goods.blade.php diff --git a/app/Http/Controllers/Admin/GoodController.php b/app/Http/Controllers/Admin/GoodController.php index 02a9899..7f3ba6f 100644 --- a/app/Http/Controllers/Admin/GoodController.php +++ b/app/Http/Controllers/Admin/GoodController.php @@ -3,8 +3,10 @@ namespace App\Http\Controllers\Admin; use App\Http\Controllers\Controller; +use App\Http\Requests\GoodsRequest; use App\Models\Good; use Illuminate\Http\Request; +use Illuminate\Support\Facades\Storage; class GoodController extends Controller { @@ -15,7 +17,9 @@ class GoodController extends Controller */ public function index() { - // + $goods = Good::query()->orderBy('id')->paginate(5); + return view('admin.goods.index', compact('goods')); + } /** @@ -25,7 +29,7 @@ class GoodController extends Controller */ public function create() { - // + return view('admin.goods.create'); } /** @@ -34,9 +38,16 @@ class GoodController extends Controller * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ - public function store(Request $request) + public function store(GoodsRequest $request) { - // + $params = $request->all(); + + if ($request->has('image')) { + $params['image'] = $request->file('image')->store('goods', 'public'); + } + + Good::create($params); + return redirect()->route('admin.goods.index'); } /** @@ -58,7 +69,7 @@ class GoodController extends Controller */ public function edit(Good $good) { - // + return view('admin.goods.edit', compact('good')); } /** @@ -70,7 +81,17 @@ class GoodController extends Controller */ public function update(Request $request, Good $good) { - // + $params = $request->all(); + + if ($request->has('image')) { + if (!empty($good->image)) Storage::delete($good->image); + $params['image'] = $request->file('image')->store('goods', 'public'); + } else { + if (!empty($good->image)) $params['image'] = $good->image; + } + + $good->update($params); + return redirect()->route('admin.goods.index'); } /** @@ -81,6 +102,10 @@ class GoodController extends Controller */ public function destroy(Good $good) { - // + if (!empty($good->image)) { + Storage::delete($good->image); + } + $good->delete(); + return redirect()->route('admin.goods.index'); } } diff --git a/app/Http/Controllers/MainController.php b/app/Http/Controllers/MainController.php index 41c16a9..01f22e5 100644 --- a/app/Http/Controllers/MainController.php +++ b/app/Http/Controllers/MainController.php @@ -2,12 +2,16 @@ namespace App\Http\Controllers; +use App\Models\Banner; +use App\Models\Category; use Illuminate\Http\Request; class MainController extends Controller { public function index() { - return view('index'); + $banners = Banner::query()->orderBy('id')->get(); + $category = Category::query()->where('parent_id', '>', '0')->orderBy('id')->get(); + return view('index', compact('banners', 'category')); } public function about_company() { @@ -15,7 +19,8 @@ class MainController extends Controller } public function catalog() { - return view('catalog'); + $category = Category::query()->orderBy('id')->get(); + return view('catalog', compact('category')); } public function good() { diff --git a/app/Http/Requests/GoodsRequest.php b/app/Http/Requests/GoodsRequest.php new file mode 100644 index 0000000..10a7285 --- /dev/null +++ b/app/Http/Requests/GoodsRequest.php @@ -0,0 +1,39 @@ + + */ + public function rules() + { + return [ + 'title' => 'required|min:3|max:255', + 'price' => 'required|min:0|numeric', + 'price_old' => 'required|min:0|numeric', + 'manufacturer' => 'required|min:0|max:255', + 'country' => 'required|min:0|max:255', + 'power' => 'required|min:0|numeric', + 'image' => [ + 'mimes:jpeg,jpg,png', + 'max:10000' + ], + ]; + } +} diff --git a/app/Models/Category.php b/app/Models/Category.php index c33148f..3d4dde3 100644 --- a/app/Models/Category.php +++ b/app/Models/Category.php @@ -10,4 +10,8 @@ class Category extends Model use HasFactory; protected $fillable = ['name', 'image', 'content', 'parent_id']; + + public function goods() { + return $this->hasMany(Good::class); + } } diff --git a/app/Models/Good.php b/app/Models/Good.php index b022ff4..4719883 100644 --- a/app/Models/Good.php +++ b/app/Models/Good.php @@ -8,4 +8,15 @@ use Illuminate\Database\Eloquent\Model; class Good extends Model { use HasFactory; + + protected $fillable = ['title', 'image', 'category_id', 'price', + 'price_old', 'manufacturer', 'country', 'size', + 'power', 'description', 'feature', 'equipment', + 'accessory_id', 'tooling_id', 'weight', 'new', + 'stock_count', 'demo', 'way', 'type_good' + ]; + + public function category() { + return $this->belongsTo(Category::class, 'category_id'); + } } diff --git a/app/Models/News.php b/app/Models/News.php index c8b24dc..c300081 100644 --- a/app/Models/News.php +++ b/app/Models/News.php @@ -8,4 +8,6 @@ use Illuminate\Database\Eloquent\Model; class News extends Model { use HasFactory; + + protected $fillable = ['title', 'image', 'text']; } diff --git a/app/Models/Project.php b/app/Models/Project.php index 1034631..9eaf97a 100644 --- a/app/Models/Project.php +++ b/app/Models/Project.php @@ -8,4 +8,6 @@ use Illuminate\Database\Eloquent\Model; class Project extends Model { use HasFactory; + + protected $fillable = ['title', 'image', 'customer', 'date_project']; } diff --git a/app/Providers/ComposerServiceProvider.php b/app/Providers/ComposerServiceProvider.php index 1340da8..1e47b04 100644 --- a/app/Providers/ComposerServiceProvider.php +++ b/app/Providers/ComposerServiceProvider.php @@ -3,6 +3,7 @@ namespace App\Providers; use App\Models\Category; +use App\Models\Good; use Illuminate\Support\Facades\View; use Illuminate\Support\ServiceProvider; @@ -20,6 +21,7 @@ class ComposerServiceProvider extends ServiceProvider 'admin.part.categories', //выбор категории поста при редактировании 'admin.part.parents', //выбор родителя категории при редактировании 'admin.part.all-ctgs', //все категории в административной части + 'admin.part.category_id', ]; View::composer($views, function($view) { @@ -29,6 +31,20 @@ class ComposerServiceProvider extends ServiceProvider } $view->with(['items' => $items]); }); + + $views = [ + 'admin.part.goods', + ]; + View::composer($views, + function($view) { + static $items = null; + if (is_null($items)) { + $items = Good::all(); + } + $view->with(['items' => $items]); + }); + + } /** diff --git a/database/migrations/2023_03_31_121853_create_goods_table.php b/database/migrations/2023_03_31_121853_create_goods_table.php index 57b004a..a11d682 100644 --- a/database/migrations/2023_03_31_121853_create_goods_table.php +++ b/database/migrations/2023_03_31_121853_create_goods_table.php @@ -19,6 +19,7 @@ return new class extends Migration $table->string('title', 255); $table->integer('price')->default(0); $table->integer('price_old')->default(0); + $table->string('image', 255)->nullable(); $table->string('manufacturer', 255)->nullable(); $table->string('country', 255)->nullable(); $table->string('size', 255)->nullable(); diff --git a/database/migrations/2023_03_31_122437_create_news_table.php b/database/migrations/2023_03_31_122437_create_news_table.php index 2aa14ac..62039dc 100644 --- a/database/migrations/2023_03_31_122437_create_news_table.php +++ b/database/migrations/2023_03_31_122437_create_news_table.php @@ -18,6 +18,9 @@ return new class extends Migration $table->string('title', 255)->nullable(); $table->text('text')->nullable(); $table->string('image', 255)->nullable(); + $table->string('status', 50)->default('новость'); + $table->string('author', 255)->nullable(); + $table->string('company', 255)->nullable(); $table->timestamps(); }); } diff --git a/resources/views/admin/goods/create.blade.php b/resources/views/admin/goods/create.blade.php new file mode 100644 index 0000000..0b3e2ab --- /dev/null +++ b/resources/views/admin/goods/create.blade.php @@ -0,0 +1,15 @@ +@extends('layout.admin', ['title' => 'Создание товара']) + +@section('content') +
+
+ + + +
+
+@endsection diff --git a/resources/views/admin/goods/edit.blade.php b/resources/views/admin/goods/edit.blade.php new file mode 100644 index 0000000..d6f6c17 --- /dev/null +++ b/resources/views/admin/goods/edit.blade.php @@ -0,0 +1,14 @@ +@extends('layout.admin', ['title' => 'Редактирование товара']) + +@section('content') +
+
+ + +
+
+@endsection diff --git a/resources/views/admin/goods/form.blade.php b/resources/views/admin/goods/form.blade.php new file mode 100644 index 0000000..900d154 --- /dev/null +++ b/resources/views/admin/goods/form.blade.php @@ -0,0 +1,237 @@ +@csrf + +@isset($good) + @method('PUT') +@endisset + +
+@error('title') +
{{ $message }}
+@enderror +

+ +
+ +
+ @php + $parent_id = old('category_id') ?? $good->category_id ?? 0; + @endphp + +
+ +
+@error('price') +
{{ $message }}
+@enderror +

+ +
+@error('price_old') +
{{ $message }}
+@enderror +

+ +
+@error('manufacturer') +
{{ $message }}
+@enderror +

+ +
+@error('country') +
{{ $message }}
+@enderror +

+ +
+@error('size') +
{{ $message }}
+@enderror +

+ +
+@error('power') +
{{ $message }}
+@enderror +

+ +
+@error('description') +
{{ $message }}
+@enderror +

+ +
+@error('feature') +
{{ $message }}
+@enderror +

+ +
+@error('equipment') +
{{ $message }}
+@enderror +

+ +
+
+ @php + $current_id = old('accessory_id') ?? $good->accessory_id ?? 0; + @endphp + +
+ +
+
+ @php + $current_id = old('tooling_id') ?? $good->tooling_id ?? 0; + @endphp + +
+ +
+@error('weight') +
{{ $message }}
+@enderror +

+ +
+@error('stock_count') +
{{ $message }}
+@enderror +

+ +
+@error('type_good') +
{{ $message }}
+@enderror +

+ +
+@error('new') +
{{ $message }}
+@enderror +
+ +
+@error('demo') +
{{ $message }}
+@enderror +
+ +
+@error('way') +
{{ $message }}
+@enderror +
+ + +
+ +@isset($category->image) +
+ +
+@endisset + + +< diff --git a/resources/views/admin/goods/index.blade.php b/resources/views/admin/goods/index.blade.php new file mode 100644 index 0000000..20cecc8 --- /dev/null +++ b/resources/views/admin/goods/index.blade.php @@ -0,0 +1,66 @@ +@extends('layout.admin', ['title' => 'Товары']) + +@section('content') +
+
+
+

+ + + + + + + + + + + + + + @if ($goods->count()) + @foreach($goods as $good) + + + + + + + + + @endforeach + @else + + + + + + + + + @endif + + +
IDФотоКатегорияТоварДата созданияДействия
{{ $good->id }}image)) {?>Нет фото + + + {{ $good->category->name }}{{ $good->title }}{{ $good->created_at }}
+ + Редактировать + | + @csrf + @method('DELETE') + +
+
------
+ + {{ $goods->onEachSide(1)->links('catalogs.paginate') }} + +
+
+

+@endsection diff --git a/resources/views/admin/news/edit.blade.php b/resources/views/admin/news/edit.blade.php index 5e67d4c..ab39b59 100644 --- a/resources/views/admin/news/edit.blade.php +++ b/resources/views/admin/news/edit.blade.php @@ -6,7 +6,7 @@ - diff --git a/resources/views/admin/part/category_id.blade.php b/resources/views/admin/part/category_id.blade.php new file mode 100644 index 0000000..6c1ebd8 --- /dev/null +++ b/resources/views/admin/part/category_id.blade.php @@ -0,0 +1,10 @@ +@if ($items->where('parent_id', $parent)->count()) + @php $level++ @endphp + @foreach ($items->where('parent_id', $parent) as $item) + + @include('admin.part.category_id', ['level' => $level, 'parent' => $item->id]) + @endforeach +@endif diff --git a/resources/views/admin/part/goods.blade.php b/resources/views/admin/part/goods.blade.php new file mode 100644 index 0000000..999b0cc --- /dev/null +++ b/resources/views/admin/part/goods.blade.php @@ -0,0 +1,7 @@ +@if ($items->count()) + @foreach ($items as $item) + + @endforeach +@endif diff --git a/resources/views/banners/index.blade.php b/resources/views/banners/index.blade.php index 6de693f..9b8c6fb 100644 --- a/resources/views/banners/index.blade.php +++ b/resources/views/banners/index.blade.php @@ -1,21 +1,25 @@ +@if ($banners->count())
+ @foreach ($banners as $banner)

- Поставляем промышленное оборудование с 2008 года + {{$banner->title}}

-

Горизонтальный фрезерный центр с ЧПУ

-

SINO HMC1000P

+

{{$banner->text}}

+
- Изображение слайдера + {{$banner->title}} +
-
+ @endforeach +
- +
- +
+@endif diff --git a/resources/views/index.blade.php b/resources/views/index.blade.php index dc9f7b6..887776a 100644 --- a/resources/views/index.blade.php +++ b/resources/views/index.blade.php @@ -14,22 +14,25 @@ + @if ($category->count())
+ @endif