GoalKeywordDelete.php 2.97 KB
<?php

namespace App\Models\Pivots;

use App\Models\Keyword;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\Pivot;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Collection;

/**
 * App\Models\Pivots\GoalKeywordDelete
 *
 * @property int $id
 * @property int $external_id
 * @property int $dictionary_campaign_external_id
 * @property int $goal_ad_group_external_id
 * @property int $dictionary_campaign_id
 * @property int $goal_ad_group_id
 * @property int $keyword_id
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property-read Keyword $keyword
 * @method static Builder|GoalKeyword newModelQuery()
 * @method static Builder|GoalKeyword newQuery()
 * @method static Builder|GoalKeyword query()
 * @method static Builder|GoalKeyword whereCreatedAt($value)
 * @method static Builder|GoalKeyword whereDictionaryCampaignExternalId($value)
 * @method static Builder|GoalKeyword whereDictionaryCampaignId($value)
 * @method static Builder|GoalKeyword whereExternalId($value)
 * @method static Builder|GoalKeyword whereExternalUpdatedAt($value)
 * @method static Builder|GoalKeyword whereExternalUploadAt($value)
 * @method static Builder|GoalKeyword whereGoalAdGroupExternalId($value)
 * @method static Builder|GoalKeyword whereGoalAdGroupId($value)
 * @method static Builder|GoalKeyword whereId($value)
 * @method static Builder|GoalKeyword whereKeywordId($value)
 * @method static Builder|GoalKeyword whereUpdatedAt($value)
 * @method static Builder|GoalKeyword whereUpdatedNeed($value)
 * @mixin \Eloquent
 */
class GoalKeywordDelete extends Pivot
{

    protected $table = 'goal_keywords_delete';

    protected $fillable = [
        'external_id',
        'goal_ad_group_external_id',
        'dictionary_campaign_external_id',
        'goal_ad_group_id',
        'dictionary_campaign_id',
        'keyword_id',
    ];

    public $incrementing = true;

   static public function updateOrCreateByMain(GoalKeyword $goalKeyword)
   {
       if (
           !$goalKeyword->external_id
           ||
           !$goalKeyword->goal_ad_group_external_id
           ||
           !$goalKeyword->dictionary_campaign_external_id
       ) {
           return null;
       }

       return GoalKeywordDelete::updateOrCreate([
           'external_id' => $goalKeyword->external_id,
           'goal_ad_group_external_id' => $goalKeyword->goal_ad_group_external_id,
           'dictionary_campaign_external_id' => $goalKeyword->dictionary_campaign_external_id,
           'goal_ad_group_id' => $goalKeyword->goal_ad_group_id,
           'dictionary_campaign_id' => $goalKeyword->dictionary_campaign_id,
           'keyword_id' => $goalKeyword->keyword_id,
       ]);
   }

    public function keyword()
    {
        return $this->belongsTo(Keyword::class, 'keyword_id');
    }

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

}