169 lines
5.4 KiB
PHP
169 lines
5.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Bo;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Bo\Games\StoreGameRequest;
|
|
use App\Http\Requests\Bo\Games\UpdateGameRequest;
|
|
use App\Models\Game;
|
|
use App\Traits\Controllers\UpdateModelPublished;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Str;
|
|
|
|
class GameController extends Controller
|
|
{
|
|
use UpdateModelPublished;
|
|
|
|
/**
|
|
* Create the controller instance.
|
|
*/
|
|
public function __construct()
|
|
{
|
|
$this->authorizeResource(Game::class);
|
|
}
|
|
|
|
/**
|
|
* 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 $gameModels */
|
|
$gameModels = Game::query();
|
|
|
|
/** @var string $search Search field */
|
|
$search = $request->search;
|
|
if ($search) {
|
|
$this->searchQuery(
|
|
$gameModels,
|
|
$search,
|
|
null,
|
|
'title_line1',
|
|
'title_line2',
|
|
'text'
|
|
);
|
|
}
|
|
$searchFields = \implode(', ', [
|
|
Str::ucfirst(trans('validation.custom.title_line1')),
|
|
trans('validation.custom.title_line2'),
|
|
trans('validation.attributes.text')
|
|
]);
|
|
|
|
/** Sort columns with a query */
|
|
$this->sortQuery($gameModels);
|
|
|
|
/** Custom pagination */
|
|
$gameModels = $this->paginate($gameModels);
|
|
|
|
return view('bo.pages.games.index', compact('gameModels', 'search', 'searchFields'));
|
|
}
|
|
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*
|
|
* @param \App\Models\Game $game
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function create(Game $game): \Illuminate\Contracts\View\View
|
|
{
|
|
return view('bo.pages.games.create', ['gameModel' => $game]);
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*
|
|
* @param \App\Http\Requests\Bo\Games\StoreGameRequest $request
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function store(StoreGameRequest $request): \Illuminate\Http\RedirectResponse
|
|
{
|
|
return DB::transaction(function () use ($request) {
|
|
$game = new Game();
|
|
$game->fill($request->validated());
|
|
|
|
if ($game->saveOrFail()) {
|
|
return redirect()->route('bo.games.edit', $game)
|
|
->with('success', __('crud.messages.has_been_created', [
|
|
'model' => Str::ucfirst(trans_choice('models.classes.game', 1))
|
|
]));
|
|
}
|
|
return redirect()->back()
|
|
->with('error', __('crud.messages.cannot_be_created', [
|
|
'model' => Str::ucfirst(trans_choice('models.classes.game', 1))
|
|
]));
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*
|
|
* @param App\Models\Game $game
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function edit(Game $game): \Illuminate\Contracts\View\View
|
|
{
|
|
return view('bo.pages.games.edit', ['gameModel' => $game]);
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*
|
|
* @param \App\Http\Requests\Bo\Games\UpdateGameRequest $request
|
|
* @param \App\Models\Game $game
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function update(UpdateGameRequest $request, Game $game): \Illuminate\Http\RedirectResponse
|
|
{
|
|
return DB::transaction(function () use ($request, $game) {
|
|
$game->fill($request->validated());
|
|
|
|
if ($game->saveOrFail()) {
|
|
return redirect()->route('bo.games.edit', $game)
|
|
->with('success', __('crud.messages.has_been_updated', [
|
|
'model' => Str::ucfirst(trans_choice('models.classes.game', 1))
|
|
]));
|
|
}
|
|
return redirect()->route('bo.games.edit', $game)
|
|
->with('error', __('crud.messages.cannot_be_updated', [
|
|
'model' => Str::ucfirst(trans_choice('models.classes.game', 1))
|
|
]));
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*
|
|
* @param \App\Models\Game $game
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function destroy(Game $game): \Illuminate\Http\RedirectResponse
|
|
{
|
|
return DB::transaction(function () use ($game) {
|
|
if ($game->deleteOrFail()) {
|
|
return redirect()->route('bo.games.index')
|
|
->with('success', __('crud.messages.has_been_deleted', [
|
|
'model' => Str::ucfirst(trans_choice('models.classes.game', 1))
|
|
]));
|
|
}
|
|
return redirect()->back()
|
|
->with('error', __('crud.messages.cannot_be_deleted', [
|
|
'model' => Str::ucfirst(trans_choice('models.classes.game', 1))
|
|
]));
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Duplicate the specified resource.
|
|
*
|
|
* @param \App\Models\Game $game
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function duplicate(Game $game): \Illuminate\Contracts\View\View
|
|
{
|
|
return $this->create($game->replicate());
|
|
}
|
|
}
|