156 lines
5.1 KiB
PHP
156 lines
5.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Bo;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Bo\StoreGroups\StoreStoreGroupRequest;
|
|
use App\Http\Requests\Bo\StoreGroups\UpdateStoreGroupRequest;
|
|
use App\Models\StoreGroup;
|
|
use App\Traits\Controllers\UpdateModelStatus;
|
|
use App\Traits\Controllers\ChangesModelOrder;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Str;
|
|
|
|
class StoreGroupController extends Controller
|
|
{
|
|
use ChangesModelOrder;
|
|
use UpdateModelStatus;
|
|
|
|
/**
|
|
* Display a listing of the resource.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function index(Request $request): \Illuminate\Contracts\View\View
|
|
{
|
|
/** @var \Illuminate\Database\Eloquent\Builder $storeGroup */
|
|
$storeGroup = StoreGroup::query();
|
|
|
|
if ($request->search) {
|
|
$this->searchQuery(
|
|
$storeGroup,
|
|
$request->search,
|
|
null,
|
|
'name',
|
|
);
|
|
}
|
|
|
|
$this->sortQuery($storeGroup);
|
|
$searchFields = trans('Name');
|
|
|
|
$storeGroupModels = $storeGroup->paginate(10);
|
|
|
|
return view('bo.pages.storegroups.index', compact('storeGroupModels', 'searchFields'));
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function create(): \Illuminate\Contracts\View\View
|
|
{
|
|
return view('bo.pages.storegroups.create');
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*
|
|
* @param \App\Http\Requests\Bo\StoreGroups\StoreStoreGroupRequest $request
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function store(StoreStoreGroupRequest $request): \Illuminate\Http\RedirectResponse
|
|
{
|
|
return DB::transaction(function () use ($request) {
|
|
$storeGroup = new StoreGroup();
|
|
$storeGroup->fill($request->validated());
|
|
|
|
if ($storeGroup->saveOrFail()) {
|
|
return redirect()
|
|
->route('bo.storegroups.edit', $storeGroup)
|
|
->with('success', trans(
|
|
'crud.messages.has_been_created',
|
|
['model' => Str::singular(trans('models.classes.storegroups'))]
|
|
));
|
|
}
|
|
return redirect()
|
|
->back()
|
|
->with('error', trans(
|
|
'crud.messages.cannot_be_created',
|
|
['model' => Str::singular(trans('models.classes.storegroups'))]
|
|
));
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Display a listing of the resource.
|
|
*
|
|
* @param \App\Models\StoreGroup $storegroup
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function edit(StoreGroup $storegroup): \Illuminate\Contracts\View\View
|
|
{
|
|
return view('bo.pages.storegroups.edit', ['storegroupModel' => $storegroup]);
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*
|
|
* @param \App\Http\Requests\Bo\StoreGroups\UpdateStoreGroupRequest $request
|
|
* @param \App\Models\StoreGroup $storegroup
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function update(UpdateStoreGroupRequest $request, StoreGroup $storegroup): \Illuminate\Http\RedirectResponse
|
|
{
|
|
return DB::transaction(function () use ($request, $storegroup) {
|
|
$storegroup->fill($request->validated());
|
|
if ($storegroup->saveOrFail()) {
|
|
return redirect()
|
|
->route('bo.storegroups.edit', $storegroup)
|
|
->with('success', trans(
|
|
'crud.messages.has_been_updated',
|
|
['model' => Str::singular(trans('models.classes.storegroups'))]
|
|
));
|
|
} //end if
|
|
return redirect()
|
|
->back()
|
|
->with('error', trans(
|
|
'crud.messages.cannot_be_updated',
|
|
['model' => Str::singular(trans('models.classes.storegroups'))]
|
|
));
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*
|
|
* @param \App\Models\StoreGroup $storegroup
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function destroy(StoreGroup $storegroup): \Illuminate\Http\RedirectResponse
|
|
{
|
|
foreach ($storegroup->storeRegions as $region) {
|
|
foreach ($region->stores as $store) {
|
|
$store->deleteOrFail();
|
|
}
|
|
$region->deleteOrFail();
|
|
}
|
|
|
|
if ($storegroup->deleteOrFail()) {
|
|
return redirect()
|
|
->route('bo.storegroups.index')
|
|
->with('success', trans(
|
|
'crud.messages.has_been_deleted',
|
|
['model' => Str::singular(trans('models.classes.storegroups'))]
|
|
));
|
|
}
|
|
return redirect()
|
|
->back()
|
|
->with('error', trans(
|
|
':crud.messages.cannot_be_deleted',
|
|
['model' => Str::singular(trans('models.classes.storegroups'))]
|
|
));
|
|
}
|
|
}
|