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') +
ID | +Фото | +Категория | +Товар | +Дата создания | +Действия | +
---|---|---|---|---|---|
{{ $good->id }} | +if (empty($good->image)) {?>Нет фото} else {?> + + + }?> | +{{ $good->category->name }} | +{{ $good->title }} | +{{ $good->created_at }} | ++ | +
- | +- | +- | +- | +- | +- | +