diff --git a/app/Http/Controllers/TableController.php b/app/Http/Controllers/TableController.php index cf2e163..14206b5 100644 --- a/app/Http/Controllers/TableController.php +++ b/app/Http/Controllers/TableController.php @@ -18,6 +18,7 @@ use DB; use Illuminate\Http\Request; use Illuminate\Validation\Rule; use MightyPork\Exceptions\NotApplicableException; +use MightyPork\Exceptions\SimpleValidationException; use MightyPork\Utils\Utils; class TableController extends Controller @@ -131,7 +132,6 @@ class TableController extends Controller 'name' => [ 'required', VALI_NAME, - Rule::unique('tables')->ignoreModel($tableModel), ], 'title' => ['required', VALI_LINE], 'description' => ['nullable', VALI_TEXT], @@ -139,6 +139,11 @@ class TableController extends Controller 'origin' => ['nullable', VALI_TEXT], ]); + $otherTableNames = $user->tables()->get(['name'])->pluck('name')->diff([$table])->all(); + if (in_array($input->name, $otherTableNames)) { + return $this->backWithErrors(['name' => "You already have a table called \"$input->name\""]); + } + $tableModel->fill($input->all()); $tableModel->save(); @@ -156,7 +161,6 @@ class TableController extends Controller 'name' => [ 'required', VALI_NAME, - Rule::unique('tables'), ], 'title' => ['required', VALI_LINE], 'description' => ['nullable', VALI_TEXT], @@ -169,7 +173,7 @@ class TableController extends Controller // Check if table name is unique for user if ($u->hasTable($input->name)) { return $this->backWithErrors([ - 'name' => "A table called \"$input->name\" already exists in your account.", + 'name' => "You already have a table called \"$input->name\"", ]); } diff --git a/app/Models/User.php b/app/Models/User.php index d661bbe..14de135 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -231,9 +231,10 @@ class User extends BaseModel implements * Check if this user has a table with the givenname * * @param string $name + * @return bool */ public function hasTable(string $name) { - $this->tables()->where('name', $name)->exists(); + return $this->tables()->where('name', $name)->exists(); } }