Files
Global-Jain/app/Models/Thana.php
2025-11-05 10:37:10 +05:30

137 lines
2.9 KiB
PHP

<?php
namespace App\Models;
use App\Models\Sant;
use App\Models\User;
use App\Models\ThanaMember;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;
class Thana extends Model
{
use HasFactory;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'sant_id',
'name',
'created_by',
'updated_by'
];
protected $guarded = [
'id',
];
/**
* Get the owner of the team.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function owner()
{
return $this->belongsTo(Sant::class, 'sant_id');
}
/**
* Get all of the team's sants including its owner.
*
* @return \Illuminate\Support\Collection
*/
public function allSants()
{
return $this->sants->merge([$this->owner]);
}
/**
* Get all of the users that belong to the team.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function thanaMember()
{
// return $this->belongsToMany(Sant::class, 'thana_members')
// ->withTimestamps();
return $this->hasMany(ThanaMember::class, 'thana_id');
}
/**
* Get all of the users that belong to the team.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function getThanaMember()
{
return $this->belongsToMany(Sant::class, 'thana_members')->withPivot('id', 'is_leader', 'is_approved')
->withTimestamps();
}
/**
* Determine if the given user belongs to the team.
*
* @param \App\Models\User $user
* @return bool
*/
public function hasUser($sant)
{
return $this->sants->contains($sant) || $sant->ownsTeam($this);
}
/**
* Remove the given user from the team.
*
* @param \App\Models\User $user
* @return void
*/
public function removeUser($sant)
{
if ($sant->current_team_id === $this->id) {
$sant->forceFill([
'current_team_id' => null,
])->save();
}
$this->sants()->detach($sant);
}
/**
* Purge all of the team's resources.
*
* @return void
*/
public function purge()
{
$this->owner()->where('current_team_id', $this->id)
->update(['current_team_id' => null]);
$this->sants()->where('current_team_id', $this->id)
->update(['current_team_id' => null]);
$this->sants()->detach();
$this->delete();
}
/**
* @return mixed
*/
public function createdBy()
{
return $this->belongsTo(User::class, 'created_by');
}
/**
* @return mixed
*/
public function updatedBy()
{
return $this->belongsTo(User::class, 'updated_by', 'id');
}
}