DictionaryCampaign.php 1.3 KB
<?php

namespace App\Models\Pivots;

use App\Models\Campaigns;
use App\Models\Dictionary;
use Illuminate\Database\Eloquent\Relations\Pivot;

class DictionaryCampaign extends Pivot
{

    protected $fillable = [
        'campaign_id',
        'dictionary_id',
        'name',
        'negative_keywords',
        'excluded_sites',
        'updated',
    ];

    static public function getWithPivot()
    {
        return [
            'name',
            'negative_keywords',
            'excluded_sites',
            'updated',
        ];
    }

    static public function copyPropertyInCampaign(Campaigns $campaign)
    {
        return collect(self::getWithPivot())
            ->filter(fn($property_name) => $property_name !== 'updated')
            ->transform(function ($property_name) use ($campaign) {
                return [
                    $property_name => $campaign->{$property_name}
                ];
            })->flatMap(fn($val) => $val)->all();
    }

    protected $cast = [
        'campaign_id' => 'int',
        'dictionary_id' => 'int',
        'updated' => 'boolean',
    ];

    public function dictionary()
    {
        return $this->belongsTo(Dictionary::class, 'dictionary_id');
    }

    public function campaign()
    {
        return $this->belongsTo(Campaigns::class, 'campaign_id');
    }

}