feat: move departments from projects to companies
Departments now belong to a Company, not a Project.
- New migration: recreates settings_departments with company_id (migrates existing data)
- Department model: company_id FK, company() relation
- Company model: departments() hasMany relation
- ProjectSetting model: removes departments() relation
- Controller: dept methods now take Company instead of ProjectSetting
- Routes: department CRUD moves to companies/{company}/departments
- View: departments section appears on each company card (purple theme);
project body is now locations-only (single column)
Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
This commit is contained in:
1 parent
9a268dc79b
commit
12e07480a0
7 files changed
+206
-153
No files matched your search
@@ -14,8 +14,8 @@ class ProjectSettingController extends Controller
|
||||
public function index()
|
||||
{
|
||||
$companies = Company::with([
|
||||
'projects.locations' => fn ($q) => $q->orderBy('name'),
|
||||
'projects.departments' => fn ($q) => $q->orderBy('name'),
|
||||
'projects.locations' => fn ($q) => $q->orderBy('name'),
|
||||
'departments' => fn ($q) => $q->orderBy('name'),
|
||||
])->orderBy('name')->get();
|
||||
|
||||
$allProjects = $companies->flatMap(fn ($c) => $c->projects);
|
||||
@@ -25,7 +25,7 @@ class ProjectSettingController extends Controller
|
||||
'total_projects' => $allProjects->count(),
|
||||
'active_projects' => $allProjects->where('is_active', true)->count(),
|
||||
'total_locations' => $allProjects->sum(fn ($p) => $p->locations->count()),
|
||||
'total_departments' => $allProjects->sum(fn ($p) => $p->departments->count()),
|
||||
'total_departments' => $companies->sum(fn ($c) => $c->departments->count()),
|
||||
];
|
||||
|
||||
return view('settings.projects.index', compact('companies', 'stats'));
|
||||
@@ -148,21 +148,21 @@ class ProjectSettingController extends Controller
|
||||
return response()->json(['ok' => true]);
|
||||
}
|
||||
|
||||
public function storeDepartment(Request $request, ProjectSetting $project)
|
||||
public function storeDepartment(Request $request, Company $company)
|
||||
{
|
||||
$validated = $request->validate(['name' => 'required|string|max:255']);
|
||||
$dept = $project->departments()->create(['name' => $validated['name'], 'is_active' => true]);
|
||||
$dept = $company->departments()->create(['name' => $validated['name'], 'is_active' => true]);
|
||||
return response()->json(['department' => ['id' => $dept->id, 'name' => $dept->name, 'is_active' => $dept->is_active]]);
|
||||
}
|
||||
|
||||
public function updateDepartment(Request $request, ProjectSetting $project, Department $department)
|
||||
public function updateDepartment(Request $request, Company $company, Department $department)
|
||||
{
|
||||
$validated = $request->validate(['name' => 'required|string|max:255']);
|
||||
$department->update(['name' => $validated['name'], 'is_active' => $request->boolean('is_active', true)]);
|
||||
return response()->json(['department' => ['id' => $department->id, 'name' => $department->name, 'is_active' => $department->is_active]]);
|
||||
}
|
||||
|
||||
public function destroyDepartment(ProjectSetting $project, Department $department)
|
||||
public function destroyDepartment(Company $company, Department $department)
|
||||
{
|
||||
$department->delete();
|
||||
return response()->json(['ok' => true]);
|
||||
|
||||
@@ -16,4 +16,9 @@ class Company extends Model
|
||||
{
|
||||
return $this->hasMany(ProjectSetting::class, 'company_id');
|
||||
}
|
||||
|
||||
public function departments(): \Illuminate\Database\Eloquent\Relations\HasMany
|
||||
{
|
||||
return $this->hasMany(Department::class, 'company_id');
|
||||
}
|
||||
}
|
||||
@@ -8,12 +8,12 @@ class Department extends Model
|
||||
{
|
||||
protected $table = 'settings_departments';
|
||||
|
||||
protected $fillable = ['name', 'project_id', 'is_active'];
|
||||
protected $fillable = ['name', 'company_id', 'is_active'];
|
||||
|
||||
protected $casts = ['is_active' => 'boolean'];
|
||||
|
||||
public function project(): \Illuminate\Database\Eloquent\Relations\BelongsTo
|
||||
public function company(): \Illuminate\Database\Eloquent\Relations\BelongsTo
|
||||
{
|
||||
return $this->belongsTo(ProjectSetting::class, 'project_id');
|
||||
return $this->belongsTo(Company::class, 'company_id');
|
||||
}
|
||||
}
|
||||
@@ -27,8 +27,4 @@ class ProjectSetting extends Model
|
||||
return $this->hasMany(\App\Models\Settings\Location::class, 'project_id');
|
||||
}
|
||||
|
||||
public function departments(): \Illuminate\Database\Eloquent\Relations\HasMany
|
||||
{
|
||||
return $this->hasMany(\App\Models\Settings\Department::class, 'project_id');
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user