75 lines
2.0 KiB
PHP
75 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
// use Illuminate\Contracts\Auth\MustVerifyEmail;
|
|
use Database\Factories\UserFactory;
|
|
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
|
use Illuminate\Database\Eloquent\Attributes\Hidden;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Illuminate\Foundation\Auth\User as Authenticatable;
|
|
use Illuminate\Notifications\Notifiable;
|
|
use Illuminate\Support\Str;
|
|
use Laravel\Fortify\TwoFactorAuthenticatable;
|
|
|
|
#[Fillable(['name', 'email', 'password'])]
|
|
#[Hidden(['password', 'two_factor_secret', 'two_factor_recovery_codes', 'remember_token'])]
|
|
class User extends Authenticatable
|
|
{
|
|
/** @use HasFactory<UserFactory> */
|
|
use HasFactory, Notifiable, TwoFactorAuthenticatable;
|
|
|
|
/**
|
|
* Get the attributes that should be cast.
|
|
*
|
|
* @return array<string, string>
|
|
*/
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'email_verified_at' => 'datetime',
|
|
'password' => 'hashed',
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @return HasMany<ConversationParticipant, $this>
|
|
*/
|
|
public function conversationParticipants(): HasMany
|
|
{
|
|
return $this->hasMany(ConversationParticipant::class);
|
|
}
|
|
|
|
/**
|
|
* @return BelongsToMany<Conversation, $this>
|
|
*/
|
|
public function conversations(): BelongsToMany
|
|
{
|
|
return $this->belongsToMany(Conversation::class, 'conversation_participants')
|
|
->withPivot(['role', 'joined_at', 'last_read_at', 'muted_until'])
|
|
->withTimestamps();
|
|
}
|
|
|
|
/**
|
|
* @return HasMany<Message, $this>
|
|
*/
|
|
public function messages(): HasMany
|
|
{
|
|
return $this->hasMany(Message::class);
|
|
}
|
|
|
|
/**
|
|
* Get the user's initials
|
|
*/
|
|
public function initials(): string
|
|
{
|
|
return Str::of($this->name)
|
|
->explode(' ')
|
|
->take(2)
|
|
->map(fn ($word) => Str::substr($word, 0, 1))
|
|
->implode('');
|
|
}
|
|
}
|