mirror of
https://github.com/openclassify/openclassify.git
synced 2026-04-14 11:12:09 -05:00
77 lines
1.8 KiB
PHP
77 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Modules\Listing\Models\Listing;
|
|
|
|
class Conversation extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
protected $fillable = [
|
|
'listing_id',
|
|
'seller_id',
|
|
'buyer_id',
|
|
'last_message_at',
|
|
];
|
|
|
|
protected $casts = [
|
|
'last_message_at' => 'datetime',
|
|
];
|
|
|
|
public function listing()
|
|
{
|
|
return $this->belongsTo(Listing::class);
|
|
}
|
|
|
|
public function seller()
|
|
{
|
|
return $this->belongsTo(User::class, 'seller_id');
|
|
}
|
|
|
|
public function buyer()
|
|
{
|
|
return $this->belongsTo(User::class, 'buyer_id');
|
|
}
|
|
|
|
public function messages()
|
|
{
|
|
return $this->hasMany(ConversationMessage::class);
|
|
}
|
|
|
|
public function lastMessage()
|
|
{
|
|
return $this->hasOne(ConversationMessage::class)
|
|
->latestOfMany()
|
|
->select([
|
|
'conversation_messages.id',
|
|
'conversation_messages.conversation_id',
|
|
'conversation_messages.sender_id',
|
|
'conversation_messages.body',
|
|
'conversation_messages.created_at',
|
|
]);
|
|
}
|
|
|
|
public function scopeForUser(Builder $query, int $userId): Builder
|
|
{
|
|
return $query->where(function (Builder $participantQuery) use ($userId): void {
|
|
$participantQuery
|
|
->where('buyer_id', $userId)
|
|
->orWhere('seller_id', $userId);
|
|
});
|
|
}
|
|
|
|
public static function buyerListingConversationId(int $listingId, int $buyerId): ?int
|
|
{
|
|
$value = static::query()
|
|
->where('listing_id', $listingId)
|
|
->where('buyer_id', $buyerId)
|
|
->value('id');
|
|
|
|
return is_null($value) ? null : (int) $value;
|
|
}
|
|
}
|