ActivityUser.php 1.53 KB
<?php

namespace App\Models;

use App\Enums\ActivityWorkModeEnum;
use App\Enums\ActivityWorkTypeEnum;
use App\Support\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Auth;

class ActivityUser extends Model
{
    use SoftDeletes;

    protected $table = 'activity_has_users';

    public $guarded = [];

    protected $casts = [
        'type' => ActivityWorkTypeEnum::class,
        'mode' => ActivityWorkModeEnum::class
    ];

    protected $hidden = ['deleted_at'];

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class)->withTrashed();
    }

    public function project(): BelongsTo
    {
        return $this->belongsTo(Project::class)->withTrashed();
    }

    public function unread()
    {
        $userId = Auth::id();
        return $this->hasOne(UserMessage::class, ['sender_id', 'activity_id'], ['user_id', 'activity_id'])->where(['type' => 3, 'receiver_id' => $userId, 'is_read' => 0, 'is_bind' => 0])->latest();
    }

    public function ShareUser()
    {
        return $this->hasOne(ActivityShareUser::class, ['activity_id', 'user_id'], ['activity_id', 'user_id']);
    }

    public function SelfPrice()
    {
        return $this->hasOne(ActivityUserPrice::class, ['activity_id', 'user_id'], ['activity_id', 'user_id']);
    }

    public function activity()
    {
        return $this->hasOne(Activity::class, 'id', 'activity_id');
    }
}