Files
monica/app/Models/Group.php
T

92 lines
2.0 KiB
PHP

<?php
namespace App\Models;
use App\Helpers\ScoutHelper;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\MorphOne;
use Laravel\Scout\Attributes\SearchUsingFullText;
use Laravel\Scout\Attributes\SearchUsingPrefix;
use Laravel\Scout\Searchable;
class Group extends Model
{
use HasFactory;
use Searchable;
protected $table = 'groups';
/**
* The attributes that are mass assignable.
*
* @var array<string>
*/
protected $fillable = [
'vault_id',
'group_type_id',
'name',
];
/**
* Get the indexable data array for the model.
*
*
* @codeCoverageIgnore
*/
#[SearchUsingPrefix(['id', 'vault_id'])]
#[SearchUsingFullText(['name'])]
public function toSearchableArray(): array
{
return [
'id' => $this->id,
'vault_id' => $this->vault_id,
'name' => $this->name,
];
}
/**
* When updating a model, this method determines if we should update the search index.
*
* @return bool
*/
public function searchIndexShouldBeUpdated()
{
return ScoutHelper::activated();
}
/**
* Get the vault associated with the group.
*/
public function vault(): BelongsTo
{
return $this->belongsTo(Vault::class);
}
/**
* Get the vault associated with the group.
*/
public function groupType(): BelongsTo
{
return $this->belongsTo(GroupType::class);
}
/**
* Get the contacts associated with the group.
*/
public function contacts(): BelongsToMany
{
return $this->belongsToMany(Contact::class);
}
/**
* Get the group's feed item.
*/
public function feedItem(): MorphOne
{
return $this->morphOne(ContactFeedItem::class, 'feedable');
}
}