GoalAdvertisement.php 6.74 KB
<?php

namespace App\Models\Pivots;

use App\Models\Advertisement;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\Pivot;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * App\Models\Pivots\GoalAdvertisement
 *
 * @property int $id
 * @property int|null $external_id
 * @property int|null $goal_v_card_external_id
 * @property int|null $dictionary_campaign_external_id
 * @property int|null $goal_ad_group_external_id
 * @property int $dictionary_campaign_id
 * @property int|null $goal_v_card_id
 * @property int $goal_ad_group_id
 * @property int $advertisement_id
 * @property \Illuminate\Support\Carbon|null $external_upload_at
 * @property \Illuminate\Support\Carbon|null $external_updated_at
 * @property \Illuminate\Support\Carbon|null $updated_need
 * @property \Illuminate\Support\Carbon|null $archived_need
 * @property \Illuminate\Support\Carbon|null $reserve_archive_at
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property-read Advertisement $advertisement
 * @method static Builder|GoalAdvertisement forExternal()
 * @method static Builder|GoalAdvertisement forNotExternal()
 * @method static Builder|GoalAdvertisement forNotReserveCreate()
 * @method static Builder|GoalAdvertisement forNotReserveUpdate()
 * @method static Builder|GoalAdvertisement forNotReserveArchive()
 * @method static Builder|GoalAdvertisement needUpdated()
 * @method static Builder|GoalAdvertisement needArchived()
 * @method static Builder|GoalAdvertisement newModelQuery()
 * @method static Builder|GoalAdvertisement newQuery()
 * @method static Builder|GoalAdvertisement query()
 * @method static Builder|GoalAdvertisement whereCreatedAt($value)
 * @method static Builder|GoalAdvertisement whereDictionaryCampaignExternalId($value)
 * @method static Builder|GoalAdvertisement whereDictionaryCampaignId($value)
 * @method static Builder|GoalAdvertisement whereExternalId($value)
 * @method static Builder|GoalAdvertisement whereExternalUpdatedAt($value)
 * @method static Builder|GoalAdvertisement whereExternalUploadAt($value)
 * @method static Builder|GoalAdvertisement whereGoalAdGroupExternalId($value)
 * @method static Builder|GoalAdvertisement whereGoalAdGroupId($value)
 * @method static Builder|GoalAdvertisement whereId($value)
 * @method static Builder|GoalAdvertisement whereAdvertisementId($value)
 * @method static Builder|GoalAdvertisement whereUpdatedAt($value)
 * @method static Builder|GoalAdvertisement whereUpdatedNeed($value)
 * @mixin \Eloquent
 */
class GoalAdvertisement extends Pivot
{
    use SoftDeletes;

    protected $table = 'goal_advertisements';

    protected $fillable = [
        'external_id',
        'goal_v_card_external_id',
        'goal_sitelink_external_id',
        'goal_ad_group_external_id',
        'dictionary_campaign_external_id',
        'goal_v_card_id',
        'goal_sitelink_id',
        'goal_ad_group_id',
        'dictionary_campaign_id',
        'advertisement_id',
        'external_upload_at',
        'external_updated_at',
        'updated_need',
        'reserve_create_at',
        'reserve_update_at',
        'archive_at',
        'archived_need',
        'reserve_archive_at',
    ];

    protected $casts = [
        'external_upload_at' => 'datetime',
        'external_updated_at' => 'datetime',
        'updated_need' => 'datetime',
        'reserve_create_at' => 'datetime',
        'reserve_update_at' => 'datetime',
        'archive_at' => 'datetime',
        'archived_need' => 'datetime',
        'reserve_archive_at' => 'datetime',
    ];

    public $incrementing = true;

    static public function getWithPivot()
    {
        return [
            'id',
            'external_id',
            'goal_v_card_external_id',
            'goal_sitelink_external_id',
            'goal_ad_group_external_id',
            'dictionary_campaign_external_id',
            'goal_v_card_id',
            'goal_sitelink_id',
            'goal_ad_group_id',
            'dictionary_campaign_id',
            'advertisement_id',
            'external_upload_at',
            'external_updated_at',
            'updated_need',
            'reserve_create_at',
            'reserve_update_at',
            'archived_need',
            'reserve_archive_at',
            'archive_at',
        ];
    }

    /**
     * @param Builder $query
     * @return Builder
     */
    public function scopeForExternal($query)
    {
        return $query->whereNotNull('external_id');
    }

    /**
     * @param Builder $query
     * @return Builder
     */
    public function scopeForNotExternal($query)
    {
        return $query->whereNull('external_id');
    }

    /**
     * @param Builder $query
     * @return Builder
     */
    public function scopeForNotReserveCreate($query)
    {
        return $query->whereNull('reserve_create_at');
    }

    /**
     * @param Builder $query
     * @return Builder
     */
    public function scopeForNotReserveUpdate($query)
    {
        return $query->whereNull('reserve_update_at');
    }

    /**
     * @param Builder $query
     * @return Builder
     */
    public function scopeNeedUpdated($query)
    {
        return $query->whereNotNull('updated_need');
    }

    /**
     * @param Builder $query
     * @return Builder
     */
    public function scopeForArchived($query)
    {
        return $query->whereNotNull('archive_at');
    }

    /**
     * @param Builder $query
     * @return Builder
     */
    public function scopeForNotArchived($query)
    {
        return $query->whereNull('archive_at');
    }

    /**
     * @param Builder $query
     * @return Builder
     */
    public function scopeNeedArchived($query)
    {
        return $query->whereNotNull('archived_need');
    }

    /**
     * @param Builder $query
     * @return Builder
     */
    public function scopeForNotReserveArchive($query)
    {
        return $query->whereNull('reserve_archive_at');
    }

    public function advertisement()
    {
        return $this->belongsTo(Advertisement::class, 'advertisement_id');
    }

    public function dictionaryCampaign()
    {
        return $this->belongsTo(DictionaryCampaign::class, 'dictionary_campaign_id');
    }

    public function goalVCard()
    {
        return $this->belongsTo(GoalVCard::class, 'goal_v_card_id');
    }

    public function goalAdExtensions()
    {
        return $this->belongsToMany(GoalAdExtension::class, GoalAdvertisementGoalAdExtension::getModel()->getTable(), 'goal_advertisement_id', 'goal_ad_extension_id')
            ->using(GoalAdvertisementGoalAdExtension::class)
            ->withPivot(GoalAdvertisementGoalAdExtension::getWithPivot())
            ->withTimestamps();
    }

    public function goalAdExtensionsForNotExternal()
    {
        return $this->goalAdExtensions()->forNotExternal();
    }

}