Files
construprogress/app/Models/Project.php
T
javier a9000d453e feat: Add company association to projects with role management
- Created Company model and migration with fields: name, tax_id, address, phone, email, website, type, notes
- Created company_project pivot table with role_in_project field
- Added relationships: Project.companies() and Company.projects()
- Created Livewire component ProjectCompanies for managing company assignments
- Added 'Companies' tab to project edit interface alongside Phases and Users tabs
- Implemented assign/remove company functionality with role selection
- Applied same permissions logic as user assignment (assign users permission or Admin role)
2026-05-13 11:20:33 +02:00

69 lines
1.6 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Project extends Model
{
use HasFactory;
protected $fillable = [
'name', 'address', 'lat', 'lng', 'start_date', 'end_date_estimated', 'status', 'created_by'
];
protected $casts = [
'start_date' => 'date',
'end_date_estimated' => 'date',
];
// Relationships
public function phases()
{
return $this->hasMany(Phase::class)->orderBy('order');
}
public function layers()
{
return $this->hasMany(Layer::class);
}
public function users()
{
return $this->belongsToMany(User::class)->withPivot('role_in_project');
}
public function companies()
{
return $this->belongsToMany(Company::class, 'company_project')
->withPivot('role_in_project')
->withTimestamps();
}
public function creator()
{
return $this->belongsTo(User::class, 'created_by');
}
public function media()
{
return $this->morphMany(Media::class, 'mediable');
}
public function images()
{
return $this->morphMany(Media::class, 'mediable')->where('category', 'image');
}
// Scope to filter accessible projects for non-admin users
public function scopeAccessibleBy($query, User $user)
{
if ($user->hasRole('Admin')) {
return $query;
}
return $query->whereHas('users', function ($q) use ($user) {
$q->where('user_id', $user->id);
});
}
}