Added check for constrained models when deleting

This commit is contained in:
Miguel Nogueira 2020-07-12 06:39:39 +01:00
parent bd0664ce0d
commit 4dc412e53c
2 changed files with 15 additions and 7 deletions

View File

@ -16,7 +16,7 @@ class Form extends Model
public function vacancies()
{
return $this->hasMany('vacancies', 'vacancyFormID', 'id');
return $this->hasMany('App\Vacancy', 'vacancyFormID', 'id');
}
public function responses()

View File

@ -80,17 +80,25 @@ class FormController extends Controller
$form = Form::find($id);
$this->authorize('delete', $form);
$deletable = true;
// TODO: Check if form is linked to vacancies before allowing deletion
if (!is_null($form))
if (!is_null($form) && !is_null($form->vacancies) && $form->vacancies->count() !== 0 || !is_null($form->responses))
{
$deletable = false;
}
if ($deletable)
{
$form->delete();
$request->session()->flash('success', 'Form deleted successfully.');
return redirect()->back();
}
else
{
$request->session()->flash('error', 'You cannot delete this form because it\'s tied to one or more applications and ranks, or because it doesn\'t exist.');
}
$request->session()->flash('error', 'The form you\'re trying to delete does not exist.');
return redirect()->back();
}