fix: 🐛 add cascadeOnDelete to project table and change the Project query in index method in ProjectController

This commit is contained in:
Nicolas Demangeat 2025-08-19 10:27:36 +02:00
parent ed4b227f9a
commit 005378de41
6 changed files with 60 additions and 6 deletions

View file

@ -23,7 +23,9 @@ class ProjectController extends Controller
public function index(Request $request): \Illuminate\Contracts\View\View
{
/** @var \Illuminate\Database\Eloquent\Builder $projectModels */
$projectModels = Project::query();
$projectModels = Project::query()->whereHas('site', function ($query) {
$query->whereNull('deleted_at');
});
/** @var string $search Search field */
$search = $request->search;

View file

@ -169,6 +169,7 @@ class SiteController extends Controller
return DB::transaction(function () use ($site) {
if ($site->deleteOrFail()) {
$site->users()->detach();
$site->project()->delete();
return redirect()
->route('bo.sites.index')
->with('success', trans(

View file

@ -57,6 +57,9 @@ class Site extends Model
static::updating(function (self $model) {
self::manageFields($model);
});
static::deleting(function ($site) {
$site->project()->get()->each->forceDelete();
});
static::deleted(function ($site) {
$site->invoices()->delete();
$site->tickets()->delete();
@ -139,11 +142,11 @@ class Site extends Model
/**
* BelongsTo Project relation
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
* @return \Illuminate\Database\Eloquent\Relations\hasMany
*/
public function project()
{
return $this->belongsTo(Project::class);
return $this->hasMany(Project::class);
}
/**

View file

@ -8,21 +8,29 @@ return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up(): void
{
Schema::table('users', function (Blueprint $table) {
$table->foreignId('favourite_site_id')->nullable()->after('gitlab_id')->constrained('sites')->onDelete('set null');
$table->foreignId('favourite_site_id')
->nullable()
->after('gitlab_id')
->constrained('sites')
->onDelete('set null');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down(): void
{
Schema::table('users', function (Blueprint $table) {
$table->dropForeign(['favourite_site_id']);;
$table->dropForeign(['favourite_site_id']);
$table->dropColumn('favourite_site_id');
});
}

View file

@ -0,0 +1,40 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('projects', function (Blueprint $table) {
$table->dropForeign(['site_id']);
$table->foreign('site_id')
->references('id')
->on('sites')
->cascadeOnDelete();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('projects', function (Blueprint $table) {
$table->dropForeign(['site_id']);
$table->foreign('site_id')
->references('id')
->on('sites');
});
}
};

View file

@ -33,7 +33,7 @@
@foreach ($projectModels as $projectModel)
<tr class="list-item border-bottom">
<td class="text-center align-middle">{{ $projectModel->name }}</td>
<td class="text-center align-middle">{{ $projectModel->site->name ?? '' }}</td>
<td class="text-center align-middle">{{ $projectModel->site->name }}</td>
<td class="text-end align-middle">
<form action="{{ route('bo.projects.destroy', $projectModel->id) }}"
method="POST"