Added check for constrained models when deleting
This commit is contained in:
parent
bd0664ce0d
commit
4dc412e53c
|
@ -16,7 +16,7 @@ class Form extends Model
|
||||||
|
|
||||||
public function vacancies()
|
public function vacancies()
|
||||||
{
|
{
|
||||||
return $this->hasMany('vacancies', 'vacancyFormID', 'id');
|
return $this->hasMany('App\Vacancy', 'vacancyFormID', 'id');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function responses()
|
public function responses()
|
||||||
|
|
|
@ -80,17 +80,25 @@ class FormController extends Controller
|
||||||
|
|
||||||
$form = Form::find($id);
|
$form = Form::find($id);
|
||||||
$this->authorize('delete', $form);
|
$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))
|
||||||
{
|
{
|
||||||
$form->delete();
|
$deletable = false;
|
||||||
|
}
|
||||||
$request->session()->flash('success', 'Form deleted successfully.');
|
|
||||||
return redirect()->back();
|
if ($deletable)
|
||||||
|
{
|
||||||
|
$form->delete();
|
||||||
|
|
||||||
|
$request->session()->flash('success', 'Form deleted successfully.');
|
||||||
|
}
|
||||||
|
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();
|
return redirect()->back();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue