'int', 'component_id' => 'int', ]; /** * The fillable properties. * * @var string[] */ protected $fillable = [ 'subscriber_id', 'component_id', ]; /** * The validation rules. * * @var string[] */ public $rules = [ 'subscriber_id' => 'required|int', 'component_id' => 'nullable|int', ]; /** * Get the subscriber relation. * * @return \Illuminate\Database\Eloquent\Relations\BelongsTo */ public function subscriber() { return $this->belongsTo(Subscriber::class); } /** * Get the component relation. * * @return \Illuminate\Database\Eloquent\Relations\HasOne */ public function component() { return $this->belongsTo(Component::class); } /** * Finds all subscriptions for a given subscriber. * * @param \Illuminate\Database\Eloquent\Builder $query * @param int $subscriber_id * * @return \Illuminate\Database\Eloquent\Builder */ public function scopeForSubscriber(Builder $query, $subscriber_id) { return $query->where('subscriber_id', '=', $subscriber_id); } /** * Finds all subscriptions for a component. * * @param \Illuminate\Database\Eloquent\Builder $query * @param int $component_id * * @return \Illuminate\Database\Eloquent\Builder */ public function scopeForComponent(Builder $query, $component_id) { return $query->where('component_id', '=', $component_id); } /** * Finds all verified subscriptions for a component. * * @param \Illuminate\Database\Eloquent\Builder $query * @param int $component_id * * @return \Illuminate\Database\Eloquent\Builder */ public function scopeIsVerifiedForComponent(Builder $query, $component_id) { return $query->select('subscriptions.*') ->join('subscribers', 'subscriptions.subscriber_id', '=', 'subscribers.id') ->where(function ($query) { $query->where('subscriptions.component_id', '=', $component_id) ->orWhere('subscribers.global'); }) ->whereNotNull('subscribers.verified_at'); } }