File: //home/marketing.cfbon.ru/public_html/app/Http/Controllers/Promotions/PromotionsController.php
<?php
namespace App\Http\Controllers\Promotions;
use App\Enums\PromotionType;
use App\Http\Controllers\Controller;
use App\Http\Requests\Promotion\PromotionRequest;
use App\Models\Promotion;
use Illuminate\View\View;
class PromotionsController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index() : View
{
return view('promotions.index', [
'sitePromotion' => [
'type' => PromotionType::SITE,
'promotions' => Promotion::where('promotion_type', PromotionType::SITE->value)->get(),
],
'mobilePromotion' => [
'type' => PromotionType::MOBILE,
'promotions' => Promotion::where('promotion_type', PromotionType::MOBILE->value)->get(),
],
]);
}
/**
* Show the form for creating a new resource.
*/
public function create() : View
{
return view('promotions.create', [
'promotionType' => collect(PromotionType::cases())
->mapWithKeys(fn($type) => [$type->value => $type->label()]),
'selectType' => request()->query('type', PromotionType::SITE->value),
]);
}
/**
* Store a newly created resource in storage.
*/
public function store(PromotionRequest $request)
{
Promotion::create([
'name' => $request->input('name'),
'title' => $request->input('title'),
'description' => $request->input('description'),
'is_active' => $request->input('is_active'),
'img' => $request->file('img'),
'promotion_type' => $request->input('promotion_type'),
]);
return redirect()->route('promotions.index')->with('success', 'Акция добавлена');
}
/**
* Show the form for editing the specified resource.
*/
public function edit(string $id)
{
$promotion = Promotion::findOrFail($id);
return view('promotions.edit', [
'promotion' => $promotion,
'promotionType' => collect(PromotionType::cases())
->mapWithKeys(fn($type) => [$type->value => $type->label()]),
]);
}
/**
* Update the specified resource in storage.
*/
public function update(PromotionRequest $request, string $id)
{
$promotion = Promotion::findOrFail($id);
$promotion->update($request->validated());
return redirect()->back()->with('success', 'Изменения успешно применены');
}
/**
* Remove the specified resource from storage.
*/
public function destroy(string $id)
{
$promotion = Promotion::findOrFail($id);
$promotion->delete();
return redirect()->route('promotions.index')->with('success', 'Акция удалена');
}
}