src/Http/Requests/PageDeleteRequest.php
<?php
namespace EscolaLms\Pages\Http\Requests;
use EscolaLms\Pages\Models\Page;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Gate;
use Illuminate\Validation\Rule;
class PageDeleteRequest extends FormRequest
{
protected function prepareForValidation()
{
parent::prepareForValidation();
$this->merge(['id' => $this->route('id')]);
}
public function authorize(): bool
{
$page = $this->getPage();
return Gate::allows('delete', $page);
}
public function rules(): array
{
return [
'id' => [
'integer',
'required',
Rule::exists(Page::class, 'id'),
],
];
}
public function getParamId()
{
return $this->route('id');
}
public function getPage(): Page
{
return Page::findOrFail($this->route('id'));
}
}