Tokens.php 3.25 KB
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

/**
 * App\Models\Tokens
 *
 * @property int $id
 * @property string $token
 * @property string $login
 * @property string $api
 * @property string|null $type
 * @property int $created_by
 * @property string|null $timestamp
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property int $limit
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Campaigns[] $campaigns
 * @property-read int|null $campaigns_count
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Dictionary[] $cities
 * @property-read int|null $cities_count
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Limits[] $limits
 * @property-read int|null $limits_count
 * @method static \Illuminate\Database\Eloquent\Builder|Tokens filter(array $filters)
 * @method static \Illuminate\Database\Eloquent\Builder|Tokens newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|Tokens newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|Tokens query()
 * @method static \Illuminate\Database\Eloquent\Builder|Tokens whereApi($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Tokens whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Tokens whereCreatedBy($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Tokens whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Tokens whereLimit($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Tokens whereLogin($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Tokens whereTimestamp($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Tokens whereToken($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Tokens whereType($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Tokens whereUpdatedAt($value)
 * @mixin \Eloquent
 */
class Tokens extends Model
{
    CONST MAIN = 'main';
    CONST GOAL = 'goal';

    protected $fillable = ['token', 'login', 'type'];

    public function scopeFilter($query, array $filters)
    {
        $query->when($filters['login'] ?? null, function ($query, $search) {
            $query->where('login', 'like', '%'.$search.'%');
        })->when($filters['type'] ?? null, function ($query, $type) {
            $query->where('type', $type);
        })->when($filters['api'] ?? null, function ($query, $api) {
            $query->where('api', $api);
        });
    }

    public function isMain()
    {
        return $this->type === $this::MAIN;
    }

    public function limits()
    {
        return $this->hasMany(Limits::class, 'token', 'id')
            ->orderBy('updated_at', 'DESC');
    }

    public function campaigns()
    {
        return $this->hasMany(Campaigns::class, 'token', 'id')
            ->orderBy('updated_at', 'DESC');
    }

    public function cities()
    {
        return $this->hasMany(Dictionary::class, 'token_id', 'id')
            ->orderBy('updated_at', 'DESC');
    }

    public function campaignsForManaged()
    {
        return $this->campaigns()->forManaged();
    }

    public function campaignsNotForManaged()
    {
        return $this->campaigns()->notForManaged();
    }
}