diff --git a/app/Models/Project.php b/app/Models/Project.php index 6735e9c..ae75af0 100644 --- a/app/Models/Project.php +++ b/app/Models/Project.php @@ -8,6 +8,7 @@ use App\Models\MacroSchedule\Activity; use App\Models\MacroSchedule\Marker; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; +use Illuminate\Support\Str; use Kwaadpepper\CrudPolicies\Enums\CrudAction; use Kwaadpepper\CrudPolicies\Enums\CrudType; use Kwaadpepper\CrudPolicies\Traits\IsCrudModel; @@ -129,12 +130,10 @@ class Project extends Model CrudAction::update(), ] ], - 'sites' => [ - 'label' => trans('Sites'), - 'type' => CrudType::belongsToMany(), - 'belongsToMany' => Site::class, - 'belongsToManyLabel' => 'name', - 'nullable' => true, + 'site_id' => [ + 'label' => Str::singular(trans('models.classes.sites')), + 'type' => CrudType::belongsTo(), + 'belongsTo' => Site::class, 'readonly' => function () { /** @var \App\Models\User */ $user = auth()->user(); @@ -176,13 +175,13 @@ class Project extends Model // * RELATIONSHIPS. /** - * BelongsToMany Site + * BelongsTo Site * - * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany + * @return \Illuminate\Database\Eloquent\Relations\BelongsTo */ - public function sites() + public function site() { - return $this->belongsToMany(Site::class, 'project_site'); + return $this->belongsTo(Site::class); } /** diff --git a/app/Models/ProjetSite.php b/app/Models/ProjetSite.php deleted file mode 100644 index e51ff3a..0000000 --- a/app/Models/ProjetSite.php +++ /dev/null @@ -1,9 +0,0 @@ - [ - 'label' => trans('models.classes.projects'), - 'type' => CrudType::belongsToMany(), - 'belongsToMany' => Project::class, - 'belongsToManyLabel' => 'name', - 'nullable' => true, - 'readonly' => function () { - /** @var \App\Models\User */ - $user = auth()->user(); - return !$user->isAdmin(); - }, - 'actions' => [ - CrudAction::viewAny(), - CrudAction::view(), - CrudAction::create(), - CrudAction::update() - ] ] ]; parent::__construct($attributes); @@ -213,13 +195,13 @@ class Site extends Model } /** - * BelongsToMany Project relation + * BelongsTo Project relation * - * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany + * @return \Illuminate\Database\Eloquent\Relations\BelongsTo */ - public function projects() + public function project() { - return $this->belongsToMany(Project::class); + return $this->belongsTo(Project::class); } /** diff --git a/database/migrations/2022_08_26_112109_change_sites_projets_relation.php b/database/migrations/2022_08_26_112109_change_sites_projets_relation.php new file mode 100644 index 0000000..e2b55d2 --- /dev/null +++ b/database/migrations/2022_08_26_112109_change_sites_projets_relation.php @@ -0,0 +1,44 @@ +get(); + Schema::table('projects', function (Blueprint $table) { + $table->foreignId('site_id')->nullable()->after('todo'); + }); + collect($project_siteIds)->map(function (object $project_site) { + DB::table('projects')->where('id', $project_site->project_id) + ->update(['site_id' => $project_site->site_id]); + }); + Schema::table('projects', function (Blueprint $table) { + $table->foreignId('site_id')->nullable(false)->change(); + $table->foreign('site_id')->on('sites')->references('id'); + }); + Schema::drop('project_site'); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('projects', function (Blueprint $table) { + $table->dropForeign('admins_post_id_foreign'); + $table->dropColumn('post_id'); + }); + } +}