2026-05-07 23:31:33 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2026-06-16 18:05:53 +02:00
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
2026-05-07 23:31:33 +02:00
|
|
|
|
|
|
|
|
class Project extends Model
|
|
|
|
|
{
|
2026-06-16 18:05:53 +02:00
|
|
|
use HasFactory, SoftDeletes;
|
2026-05-07 23:31:33 +02:00
|
|
|
|
|
|
|
|
protected $fillable = [
|
2026-06-16 18:05:53 +02:00
|
|
|
'name', 'reference', 'address', 'country', 'lat', 'lng',
|
|
|
|
|
'start_date', 'end_date_estimated', 'status', 'created_by',
|
2026-05-07 23:31:33 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
protected $casts = [
|
2026-05-25 19:08:06 +02:00
|
|
|
"start_date" => "date",
|
|
|
|
|
"end_date_estimated" => "date",
|
2026-05-07 23:31:33 +02:00
|
|
|
];
|
|
|
|
|
|
2026-05-25 19:08:06 +02:00
|
|
|
public function changeOrders()
|
|
|
|
|
{
|
|
|
|
|
return $this->hasMany(ChangeOrder::class);
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-07 23:31:33 +02:00
|
|
|
// 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');
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-13 11:20:33 +02:00
|
|
|
public function companies()
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsToMany(Company::class, 'company_project')
|
|
|
|
|
->withPivot('role_in_project')
|
|
|
|
|
->withTimestamps();
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-07 23:31:33 +02:00
|
|
|
public function creator()
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsTo(User::class, 'created_by');
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-09 22:28:20 +02:00
|
|
|
public function media()
|
|
|
|
|
{
|
|
|
|
|
return $this->morphMany(Media::class, 'mediable');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function images()
|
|
|
|
|
{
|
|
|
|
|
return $this->morphMany(Media::class, 'mediable')->where('category', 'image');
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-07 23:31:33 +02:00
|
|
|
// 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);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|