Change relation PROJECTS SITES

This commit is contained in:
Jérémy Munsch 2022-08-26 11:46:00 +02:00
parent 889296bef3
commit 368785fb26
No known key found for this signature in database
GPG key ID: 18361AF3BFEC0FEC
4 changed files with 57 additions and 41 deletions

View file

@ -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);
}
/**

View file

@ -1,9 +0,0 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Relations\Pivot;
class ProjetSite extends Pivot
{
}

View file

@ -144,24 +144,6 @@ class Site extends Model
CrudAction::create(),
CrudAction::update(),
]
],
'projects' => [
'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);
}
/**

View file

@ -0,0 +1,44 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
class ChangeSitesProjetsRelation extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
$project_siteIds = DB::table('project_site')->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');
});
}
}