$replies * @property-read int|null $replies_count * @property-read \App\Models\User|null $responsibleUser * @property-read \App\Models\Unit|null $targetUnit * @method static \Illuminate\Database\Eloquent\Builder|Kanban newModelQuery() * @method static \Illuminate\Database\Eloquent\Builder|Kanban newQuery() * @method static \Illuminate\Database\Eloquent\Builder|Kanban onlyTrashed() * @method static \Illuminate\Database\Eloquent\Builder|Kanban query() * @method static \Illuminate\Database\Eloquent\Builder|Kanban visibleToUnit(int $unitId) * @method static \Illuminate\Database\Eloquent\Builder|Kanban whereCreatedAt($value) * @method static \Illuminate\Database\Eloquent\Builder|Kanban whereCreatedByUserId($value) * @method static \Illuminate\Database\Eloquent\Builder|Kanban whereDeletedAt($value) * @method static \Illuminate\Database\Eloquent\Builder|Kanban whereDescription($value) * @method static \Illuminate\Database\Eloquent\Builder|Kanban whereDueDate($value) * @method static \Illuminate\Database\Eloquent\Builder|Kanban whereId($value) * @method static \Illuminate\Database\Eloquent\Builder|Kanban whereOrder($value) * @method static \Illuminate\Database\Eloquent\Builder|Kanban whereOrigin($value) * @method static \Illuminate\Database\Eloquent\Builder|Kanban wherePhase($value) * @method static \Illuminate\Database\Eloquent\Builder|Kanban wherePriority($value) * @method static \Illuminate\Database\Eloquent\Builder|Kanban whereResponsibleUserId($value) * @method static \Illuminate\Database\Eloquent\Builder|Kanban whereScope($value) * @method static \Illuminate\Database\Eloquent\Builder|Kanban whereSector($value) * @method static \Illuminate\Database\Eloquent\Builder|Kanban whereTargetUnitId($value) * @method static \Illuminate\Database\Eloquent\Builder|Kanban whereTitle($value) * @method static \Illuminate\Database\Eloquent\Builder|Kanban whereUnitId($value) * @method static \Illuminate\Database\Eloquent\Builder|Kanban whereUpdatedAt($value) * @method static \Illuminate\Database\Eloquent\Builder|Kanban withTrashed(bool $withTrashed = true) * @method static \Illuminate\Database\Eloquent\Builder|Kanban withoutTrashed() * @mixin \Eloquent */ class Kanban extends Model { use HasFactory, SoftDeletes; protected $table = 'kanbans'; protected $guarded = ['id']; protected $casts = [ 'created_at' => 'datetime', 'updated_at' => 'datetime', 'due_date' => 'date', ]; // Relationships public function applicantUnit() { return $this->belongsTo(Unit::class, 'unit_id'); } public function targetUnit() { return $this->belongsTo(Unit::class, 'target_unit_id'); } public function responsibleUser() { return $this->belongsTo(User::class, 'responsible_user_id'); } public function createdByUser() { return $this->belongsTo(User::class, 'created_by_user_id'); } public function replies() { return $this->hasMany(KanbanReply::class, 'kanban_id'); } // Query Scopes public function scopeVisibleToUnit($query, int $unitId) { return $query->where(function ($q) use ($unitId) { // Cards created by this unit (origin = unit) $q->where('unit_id', $unitId) // Cards explicitly targeted at this unit (specific or broadcast-per-unit) ->orWhere('target_unit_id', $unitId); // Note: scope='all' is NOT used here — when broadcasting the controller // already creates one card per unit each with its own target_unit_id, // so filtering by target_unit_id is sufficient and correct. }); } }