ActivityLinkPivot.php 1.34 KB
<?php

namespace App\Models\Pivots;


use App\Models\Activity;
use App\Models\User;
use EloquentFilter\Filterable;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphPivot;
use Illuminate\Database\Eloquent\Relations\MorphTo;


/**
 * @method static Builder manager(int $userId)
 */
class ActivityLinkPivot extends MorphPivot
{
    use Filterable;

    protected $table = 'activity_links';

    protected $hidden = ['link_type'];

    protected $casts = ['expand' => 'json'];

    public $timestamps = false;

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

    /**
     * @return \Illuminate\Database\Eloquent\Relations\MorphTo
     */
    public function link(): MorphTo
    {
        return $this->morphTo()->withTrashed();
    }

    /**
     * @param \Illuminate\Database\Eloquent\Builder $builder
     * @param int                                   $userId
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function scopeManager(Builder $builder, int $userId): Builder
    {
        return $builder->where('type', 'manager')->where('link_type', User::class)->where('link_id', $userId);
    }

}