80 lines
2 KiB
PHP
80 lines
2 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Enums\Claims\ClaimMessageType;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
/**
|
|
* @property integer $id Subject id.
|
|
* @property string $label Subject label.
|
|
* @property \App\Models\Claim $claim_id Subject associated Claim.
|
|
* @property-read \Illuminate\Support\Carbon $created_at Subject created date.
|
|
* @property-read \Illuminate\Support\Carbon $updated_at Subject updated date.
|
|
*/
|
|
class ClaimMessage extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var array<int, string>
|
|
*/
|
|
protected $fillable = [
|
|
'content',
|
|
'type',
|
|
'claim_id',
|
|
'user_id',
|
|
];
|
|
|
|
/**
|
|
* The attributes that should be cast.
|
|
*
|
|
* @var array<string, mixed>
|
|
*/
|
|
protected $casts = [
|
|
'type' => ClaimMessageType::class,
|
|
];
|
|
|
|
// * RELATIONS
|
|
|
|
/**
|
|
* Claim belongs to relation.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
*/
|
|
public function claim(): \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
{
|
|
return $this->belongsTo(Claim::class);
|
|
}
|
|
|
|
/**
|
|
* User belongs to relation.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
*/
|
|
public function user(): \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class);
|
|
}
|
|
|
|
|
|
// * METHODS
|
|
|
|
/**
|
|
* Get all messages from a specific claim.
|
|
*
|
|
* @param integer $claimModelKey
|
|
* @return \Illuminate\Database\Eloquent\Collection
|
|
*/
|
|
public static function getAllClaimMessages(int $claimModelKey): \Illuminate\Database\Eloquent\Collection
|
|
{
|
|
return self::query()
|
|
->with('user')
|
|
->whereHas('claim', fn($q) => $q->whereKey($claimModelKey))
|
|
->orderby('created_at', 'ASC')
|
|
->get();
|
|
}
|
|
}
|