Server : Apache System : Linux 145.162.205.92.host.secureserver.net 5.14.0-611.45.1.el9_7.x86_64 #1 SMP PREEMPT_DYNAMIC Wed Apr 1 05:56:53 EDT 2026 x86_64 User : tradze ( 1001) PHP Version : 8.1.34 Disable Function : NONE Directory : /home/tradze/public_html/app/Modules/Users/Models/ |
<?php
namespace App\Modules\Users\Models;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use App\Modules\Users\Models\Participant;
class Thread extends Model
{
/**
* The database table used by the model.
*
* @var string
*/
protected $table = "messenger_threads";
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['user_id', 'request_body'];
/**
* Messages relationship.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*
* @codeCoverageIgnore
*/
public function messages()
{
return $this->hasMany('App\Modules\Users\Models\Message', 'thread_id', 'id');
}
/**
* Returns the latest message from a thread.
*
* @return null|\Cmgmyr\Messenger\Models\Message
*/
public function getLatestMessageAttribute()
{
return $this->messages()->latest()->first();
}
public function getClientUserAttribute()
{
return $this->client->first();
}
/**
* Participants relationship.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*
* @codeCoverageIgnore
*/
public function participants()
{
return $this->hasMany('App\Modules\Users\Models\Participant', 'thread_id', 'id');
}
public function latestMessage()
{
return $this->hasOne(
'App\Modules\Users\Models\Message',
'thread_id',
'id'
)->orderBy('created_at', 'desc');
}
// CUSTOMER
public function client()
{
return $this->belongsToMany(
'App\User',
'messenger_participants', // participants table
'thread_id',
'user_id'
)
->whereHas('roles', function ($q) {
$q->where('slug', 'customer');
})
->without('userDoc')
->select('users.id', 'users.name') // select only needed columns
->with('profile'); // eager-load profile for the accessor
}
// PROVIDER
public function provider()
{
return $this->belongsToMany(
'App\User',
'messenger_participants',
'thread_id',
'user_id'
)
->whereHas('roles', function ($q) {
$q->whereIn('slug', ['provider', 'therapist']);
})
->without('userDoc')
->select('users.id', 'users.name') // select only needed columns
->with('profile'); // eager-load profile for the accessor
}
public function addParticipant($userId)
{
$userIds = is_array($userId) ? $userId : func_get_args();
collect($userIds)->each(function ($userId) {
Participant::firstOrCreate([
'user_id' => $userId,
'thread_id' => $this->id,
]);
});
}
}