124 lines
3.3 KiB
PHP
124 lines
3.3 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\admin;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\AddOnRequest;
|
|
use App\Models\Addon;
|
|
use App\Repositories\Addon\AddonRepositoryInterface;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Yajra\DataTables\Facades\DataTables;
|
|
|
|
class AddonController extends Controller
|
|
{
|
|
//
|
|
public function __construct(
|
|
public AddonRepositoryInterface $addonRepository
|
|
) {
|
|
}
|
|
public function index(Request $request)
|
|
{
|
|
$addOns = $this->addonRepository->filter(
|
|
[
|
|
'page' => $request->get('page', 1),
|
|
]
|
|
);
|
|
$title = 'List Add-On';
|
|
|
|
return view('administrator.pages.addon.index', compact('addOns', 'title'));
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
$title = 'Add new addon';
|
|
return view('administrator.pages.addon.add', compact('title'));
|
|
}
|
|
|
|
public function store(AddOnRequest $request)
|
|
{
|
|
$request->validated();
|
|
|
|
DB::beginTransaction();
|
|
try {
|
|
$this->addonRepository->create($request->all());
|
|
DB::commit();
|
|
} catch (\Exception $error) {
|
|
DB::rollBack();
|
|
return back()->withErrors($error->getMessage());
|
|
}
|
|
|
|
return redirect()->route('addon.index')->with('success', 'Added successfully');
|
|
}
|
|
public function edit($id = 0)
|
|
{
|
|
|
|
$addon = $this->addonRepository->find($id);
|
|
if (!$addon) {
|
|
return back()->withErrors('Invalid request');
|
|
}
|
|
|
|
$title = 'Edit Addon';
|
|
|
|
return view('administrator.pages.addon.edit', compact('title', 'addon'));
|
|
}
|
|
public function update(int $id = 0, AddOnRequest $request)
|
|
{
|
|
$addon = $this->addonRepository->find($id);
|
|
if (!$addon) {
|
|
return back()->withErrors('Invalid request');
|
|
}
|
|
$request->validated();
|
|
|
|
DB::beginTransaction();
|
|
try {
|
|
$this->addonRepository->update($id, $request->all());
|
|
DB::commit();
|
|
} catch (\Exception $error) {
|
|
DB::rollBack();
|
|
return back()->withErrors($error->getMessage());
|
|
}
|
|
|
|
return redirect()->route('addon.index')->with('success', 'Updated successfully');
|
|
}
|
|
public function destroy($id)
|
|
{
|
|
$addon = $this->addonRepository->find($id);
|
|
if (!$addon) {
|
|
return back()->withErrors('Invalid request');
|
|
}
|
|
DB::beginTransaction();
|
|
try {
|
|
$this->addonRepository->delete($id);
|
|
DB::commit();
|
|
} catch (\Exception $error) {
|
|
DB::rollBack();
|
|
return back()->withErrors($error->getMessage());
|
|
}
|
|
|
|
return redirect()->route('addon.index')->with('success', 'Deleted successfully');
|
|
}
|
|
|
|
|
|
public function datatable(Request $request)
|
|
{
|
|
$query = $this->addonRepository->filter(
|
|
$request->all(),
|
|
[],
|
|
[],
|
|
['*'],
|
|
true
|
|
);
|
|
|
|
return DataTables::eloquent($query)
|
|
->addColumn(
|
|
'action',
|
|
function ($d) {
|
|
return view('administrator.pages.addon.datatable.action', ['row' => $d])->render();
|
|
}
|
|
)
|
|
->rawColumns(['status', 'action'])
|
|
->make(true);
|
|
}
|
|
}
|