Add Missing Foreign Keys

This commit is contained in:
Jérémy Munsch 2022-06-15 14:09:52 +02:00
parent 3d6b775d3d
commit b4797d92fc
No known key found for this signature in database
GPG key ID: 18361AF3BFEC0FEC
6 changed files with 69 additions and 16 deletions

View file

@ -85,7 +85,9 @@ class DomainNameController extends Controller
*/
public function destroy(DomainName $domain_name)
{
$emails = EmailAddress::where('domain_name_id', '=', $domain_name->id)->get();
$emails = EmailAddress::whereHas('domainName', function ($q) use ($domain_name) {
$q->whereId($domain_name->id);
})->get();
return view('domain-names.delete', [
'domain_name' => $domain_name,
'isUsed' => $emails->count() ? $emails : false

View file

@ -37,7 +37,7 @@ class StoreServerRequest extends FormRequest
'title' => 'required|min:2|max:50',
'name' => 'required|min:2|max:50',
'function' => 'required|min:2|max:50',
'datastore_pcc_id' => 'required|exists:datastore_pcc,id',
'datastore_pcc_id' => 'required|exists:datastore_pccs,id',
'comment' => 'nullable|string',
'type' => [
'required', new EnumIsValidRule(ServerType::class)

View file

@ -6,13 +6,6 @@ use Illuminate\Database\Eloquent\Model;
class DatastorePcc extends Model
{
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'datastore_pcc';
/**
* The attributes that are mass assignable.
*

View file

@ -28,4 +28,16 @@ class EmailAddress extends Model
$dn = DomainName::findOrFail($this->domain_name_id);
return $dn->utf8_name;
}
// * RELATIONSHIPS
/**
* BelongsTo domain name relation
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function domainName()
{
return $this->belongsTo(DomainName::class);
}
}

View file

@ -6,13 +6,6 @@ use Illuminate\Database\Eloquent\Model;
class ProjectsStatusChecker extends Model
{
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'projects_status_checker';
/**
* The attributes that should be cast.
*

View file

@ -0,0 +1,53 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddMissingForeignKeys extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('email_addresses', function (Blueprint $table) {
$table->foreign('domain_name_id')->references('id')
->on('domain_names');
});
Schema::rename('projects_status_checker', 'project_status_checkers');
Schema::rename('datastore_pcc', 'datastore_pccs');
Schema::table('projects', function (Blueprint $table) {
$table->foreign('prod_db_server_id')->references('id')
->on('servers');
$table->foreign('dev_db_server_id')->references('id')
->on('servers');
$table->foreign('prod_web_server_id')->references('id')
->on('servers');
$table->foreign('dev_web_server_id')->references('id')
->on('servers');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('email_addresses', function (Blueprint $table) {
$table->dropForeign(['domain_name_id']);
});
Schema::table('projects', function (Blueprint $table) {
$table->dropForeign(['prod_db_server_id']);
$table->dropForeign(['dev_db_server_id']);
$table->dropForeign(['prod_web_server_id']);
$table->dropForeign(['dev_web_server_id']);
});
Schema::rename('project_status_checkers', 'projects_status_checker');
Schema::rename('datastore_pccs', 'datastore_pcc');
}
}