Files
ABSDEV/Modules/Authors/Http/Controllers/AuthorController.php
T
2026-07-24 14:59:44 +01:00

58 lines
1.9 KiB
PHP

<?php
namespace Modules\Authors\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Models\User;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;
use Modules\Authors\Models\Author;
final class AuthorController extends Controller
{
public function index(): View
{
return view('authors::admin.index', ['records' => Author::with('user')->latest()->paginate(25)]);
}
public function create(): View
{
return view('authors::admin.form', ['users' => User::orderBy('name')->get()]);
}
public function store(Request $r): RedirectResponse
{
Author::create($this->data($r));
return redirect()->route('administration.authors.index')->with('status', 'Author created.');
}
public function edit(Author $author): View
{
return view('authors::admin.form', ['record' => $author, 'users' => User::orderBy('name')->get()]);
}
public function update(Request $r, Author $author): RedirectResponse
{
$author->update($this->data($r));
return redirect()->route('administration.authors.index')->with('status', 'Author updated.');
}
public function destroy(Author $author): RedirectResponse
{
$author->delete();
return back()->with('status', 'Author deleted.');
}
private function data(Request $r): array
{
$d = $r->validate(['user_id' => ['nullable', 'exists:users,id'], 'name' => ['required', 'string', 'max:255'], 'slug' => ['required', 'string', 'max:255'], 'job_title' => ['nullable', 'string', 'max:255'], 'biography' => ['nullable', 'string'], 'avatar_path' => ['nullable', 'string', 'max:255'], 'meta_title' => ['nullable', 'string', 'max:255'], 'meta_description' => ['nullable', 'string'], 'is_active' => ['sometimes', 'boolean']]);
$d['is_active'] = $r->boolean('is_active');
return $d;
}
}